Custom UITableViewCell SubView Initialisation? - iphone

Rather than for example doing this:
UILabel *lbl = [[UILabel alloc] init];
In the cellForRowAtIndexPath which I have discovered it VERY slow and can cause scrolling on your table view to stutter, where would I initialise my subviews such as labels?

I believe it's better to do that in a subclass of UITableViewCell. By doing so, it will be recycled with the cell.
Besides, it could be useful to set UITableViewCell and, as much as possible, its subviews' opaque to YES.
Check this two posts for more information: 1, 2

Related

TableCellView with custom xib not loading

Have have created a child of UITableViewCell, MenuItem which contains functionality for my custom table cell. I have also created a xib-file with a custom cell and set the UITableViewCell in the xib-file to have the class MenuItem. The name of the xib-file is MenuItem as well. In my UITableViewController class I'm doing this in ViewDidLoad:
UINib *menuItems = [UINib nibWithNibName:#"MenuItem" bundle:nil];
[[self tableView] registerNib:menuItems forCellReuseIdentifier:#"MenuItem"];
But the background color used in the xib-file isn't used in the cells the table displays. If I add a new label, it shows up, but the background color is just plain white. Does anyone know why?
EDIT: I do use [tableView dequeueReusableCellWithIdentifier:#"MenuItem"]; in cellForRowAtIndexPath.
Thanks to Sudha for links to the answer.
It turns out you can't set the background color in the xib-file. You have to do it in the willDisplayCell of the delegate. Read http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html, outlined box close to the top.

Adding UIPickerView to a UITableViewCell

I want to add a UIPickerView to a UITableViewCell. Right now I am just getting a black window. Can someone also explain me the concept of subclassing UI objects to a cell ie:when do we make our cell as the delegate and the datasource delegate? Thanks
EDIT: Here is the code of what i am doing right now
case 1: {
cell = [tableView dequeueReusableCellWithIdentifier:#"groups"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"groups"];
}
/*NSString *myGroupDetail = [_groupArray objectAtIndex:indexPath.row];
NSLog(#"the group detail label is %#",myGroupDetail);
cell.textLabel.text = myGroupDetail;
*/
[cell addSubview:_groupPicker];
break;
}
The groups is the section in which I want the picker view, I am getting that data from an array.
you would have to allocate and initialize a pickerview in your cellForRow method of the tableviewdelegate. ill sketch it for you =) how to initialize a cell itself should not be hard to find out if you google a bit ;-)
...(tableView *)... cellForRowAtIndexPath... {
if(indexPath.row == pickerRow){
UIPickerView *pickerView = [[UIPickerView alloc]init];
cell = ... // alloc and initialize a cell
cell addSubview:pickerView];
}
else{ // your other cells }
return cell;
}
UPDATE: im currently having trouble with git, so i uploaded a sample project to my private server: UITablePicker example
github: https://github.com/sebamisc/UItableViewWithPicker
yu can modify and use the code however you want =)
sebastian
Well, I never did exactly that.
Does it have to be in a cell or could you use the table's header or footer view for that? (I would not suggest a section header/footer view.)
Assuming it is within a UITableViewCell.
Yes, I personally would subclass UITableViewCell. Did that a lot. In that case you cell object could be the data source delegate of the picker. For doing so your subclass of UITableViewCell (let's assume you name it MyTableViewCell) needs to fulfil the related protocol. You add that UIPickerView programmatically within the init Method (initWithStyle) of MyTableViewCell. For the layout within the table cell, you should overwrite the method layoutSubviews. If your app can be rotated to landscape and portrait orientations and/or if your app is designed to run on iPad as well, then this method should dynamically consider the table's bounds. (Screen or windwo bounds are often used hiere but that is not save when the table is displayed within a split master view or a popup view on iPads.)
Strictly spoken your MyTableViewCell should not be the data source delegate, simply because it is a view element and view objects are not supposed to manage any business logic within an MVC design pattern. Smarter would be the implementation of some dedicated view controller for your table view cell hat fulfills the protocol and is assigned as the delegate. However, both would work. In the event that it is the only picker view within your sell, then you could easily use your UITableViewController subclass even without tagging the UIPickerView.
An alternative to subclassing a UITableViewCell is to create the UIPickerView within the cellForRowAtIndexPath method of your tableViewController. That is fine for singe-orientation apps. However, you may setup it in a way that it re-arranges its the UIPickerView automatically.
In any case you should overwrite the heightForRowAtIndexPath method of UITableViewController when your table views do not have the same hight any more. If all of them still have the same height, then you can simply set the rowHeight property of your UITableView.

how to disable the table cell in my tableview?

i have a my requirement disable the table cell.i have controls on that cell like(textFeild,imageview,button).when i access one control it prefprms another action so plz help me . thanks in advance.
It is not clear what you want exactly. If you just want to disable the cell i think you mean this
cell.userInteractionEnabled = NO;
But what do you mean by when i access one control it performs another action ?
I would suggest using custom UITableViewCells. Put all your controls on custom cells, that way all the events pertaining to that controls would be called in the actions in your custom cell class. In the tableView class you just don't have to give any implementation for didSelectRowAtIndexPath. That way your controls on the cells would be clickable.
It could also apply a color effect:
UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
[lab setBackgroundColor:[UIColor lightGrayColor]];
cell.backgroundView = lab;

How to nest a UITextField within a UITableViewCell?

I'm just starting out working on my first iOS app. How do you create a UITableViewCell that contains a UITextField that looks like the Title and Location fields when adding an event within the Calendar application? Are there any handy third-party components for doing this?
I can see that the table view has two grouped items and that the text fields have some placeholder text, it's more about how to go about making the text fields take up 100% of their parent table view cells.
Thanks in advance for any help.
You need to add the UITextView as a subview of the UITableViewCell. In the cellForRowAtIndexPath: method do the following:
Create a UITextView as you normally would:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,300,40)];
NB: the numbers are the x, y, width, height. Modify these to fit your own app.
Add it as a subview of the cell:
[cell addSubview:textView];
If you only want specific cells to have the textview you will need to do something like
//Use the if statement to specify which row(s) you want the UITextView to appear in
if (indexPath.row == 1) {
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0,0,300,40)];
[cell addSubview:textView];
}
The Table View Programming Guide for iOS has a section called A Closer Look at Table-View Cells. It describes two techniques for customizing a table-view cell: with code and using a pre-built nib. I think it has a lot of information you will be interested in.

Editable table view cell

I am creating an application. I have to implement a bookmark feature, and adding one should be similar to this:
mmm http://img534.imageshack.us/img534/9859/schermafbeelding2010040a.png
I want editable UITableViewCells for text input. I was wondering if there is an easier way then embedding a UITextField into a UITableViewCell. And if not, can someone explain how I can use the UITextField inside it? Thanks
There isn't an easier way, you will have to subclass UITableViewCell and add in the UITextField. Here is a tutorial for using a UITextView should be about the same as a UITextField.
You can just add a textfield to the contentView of your cell, and an (X) button to the accessoryView of your cell. You could do that in a UITableView subclass which would let you add keep track of your added elements with instance variables or properties.
I found this helpful: Cocoa With Love: Easy custom UITableView drawing
Like this:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"myEditableCellId"];
label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
[cell.contentView addSubview:label];
You can use https://github.com/escoz/QuickDialog for simple cases like that. But you probably need to write your own custom cell if it's more complex.