Why does a custom TableViewCell not need the File Owner? - iphone

I was trying to create a custom UITableViewCell in Interface Builder and kept setting the File's Owner and Custom Class of the actual UITableViewCell to my new custom UITableViewCell Class. I would hook up the IBOutlets from the File's Owner and get errors when it came to:
TVCell *cell = (TVCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TVCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[TVCell class]])
{
cell = (TVCell *)currentObject;
break;
}
}
Finally I realized you have to hook up the IBOutlets from the UITableViewCell Object, and not the File's Owner. Why is this?
Thanks

The File's Owner is a placeholder object for the object that will eventually load the NIB. It's a way for objects outside the NIB to refer to objects inside the NIB. In your case, you're trying to create the table view cell from the NIB, so you'll need some other object to be the owner. The table view cell can't both be outside and inside the NIB.
In this line of your code:
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TVCell"
owner:nil
options:nil];
You get to specify an object for the File's Owner placeholder in Interface Builder to resolve to. I'm guessing your code is in a class like 'MyTableViewController'. If it is, you could pass 'self' for the owner parameter to -[NSBundle loadNibNamed:owner:]. If you did that, you could have outlets on the MyTableViewController class that would be useful for loading this NIB. Specifically, you could use them to avoid the for loop you have. You'd do that like this:
Add a 'loadedTableViewCell' outlet to MyTableViewController
In the table cell nib, set the file's owner to MyTableViewController.
Make a connection for 'loadedTableViewCell' from the file's owner to the table view cell.
Then change your code to be similar to this:
TVCell *cell = (TVCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TVCell" owner:self options:nil];
cell = [self loadedTableViewCell];
[self setLoadedTableViewCell:nil];
}

It's because you're pulling objects out of the nib, not using the whole nib, like you would if you were loading a controller. That's what the for (id currentObject in topLevelObjects) does.

Related

UITableView CustomCell crash on Button Click

This is very common question at SO though I have google and cross check my code few times but I am not able to figure out the crash
*** -[MyCustomCell performSelector:withObject:withObject:]: message sent to deallocated instance 0x96f6980
I have a CustomCell named MyCustomCell with XIB where I have 3 buttons for Facebook,Twitter,LinkedIn. I gave them all IBAction in CustomCell class.
When I click on any of them I get this kind of crash.
I am using ARC.
In my ViewController class I have cellForRowAtIndexPath
{
static NSString *CellIdentifier = #"MyCustomCell";
MyCustomCell *cell = (MyCustomCell*)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (MyCustomCell *)[nib objectAtIndex:1];
}
return cell;
}
MyCustomCell.m
- (IBAction)facebookPressed:(BaseButton *)sender {
}
- (IBAction)twitterPressed:(BaseButton *)sender {
}
- (IBAction)linkedInPressed:(BaseButton *)sender {
}
I made this mistake just today! :) all you gotta do is replace the 2 lines
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"NameOfCustomCellNibFile" owner:self options:nil];
cell = [nib objectAtIndex:1];
You have to load the loadNibNamed: with the .xib file's name. I also thought it was with the identifier, but that is regarding the name that is referenced into the cell instead of the nib for the cell.
Hope this helps!
you can also code for Custom cell like this way. At nib of custom cell drag one UIButton and set it's tag in Xib. then use bellow Method:-
UIButton *btnMulSelected;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier =[NSString stringWithFormat:#"%d_%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle]loadNibNamed:#"cell_custome_iphones" owner:self options:nil];
cell = self.tblCell;
self.tblCell = nil;
btnMulSelected =(UIButton*)[cell.contentView viewWithTag:2];
[btnMulSelected addTarget:self action:#selector(MyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
Do not connect IBAction from Nib. just connect Custom-cell IBOutlate or put Unique UIButton tag from nib.
You've to keep the class name of cell and the XIB names same.
Keep the owner nil generally.
Also, if there is only 1 view ( i.e. 1 UITableViewCell in your case ), then this view is available at index 0 instead of index 1.
Inside the XIB file, make sure you link the button to your view itself, i.e. to IBOutlet or IBAction.
The linked buttons must be already added to your view, not floating outside in XIB. Otherwise they will instantiate and come at other indices of the loaded array. If you don't want a certain button for some time, then just keep it hidden.
So the code can be as below:
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"MyCustomCell" owner: nil options:nil];
MyCustomCell *cell = (MyCustomCell*)[views objectAtIndex:0];
Further make sure you do the following for creating XIB
Ok, then here goes the other portion simple process:
Create a MyCustomCell class derived from UITableViewCell class.
Create a separate .xib file with same name as MyCustomCell.xib.
Open .xib and delete the existing view in it. drag and drop a new
UITableViewCell from the objects of editor.
See the image to change the class name of the UITableViewCell in XIB to your class name.
Now the objectAtIndex 0 will return MyCustomCell.
Add all your buttons and other views in this UITableViewCell of your XIB.

Custom Table Cell Loading Data Objective C iOS -

Currently I have the following method, but it doesn't quite work...
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TVCell" owner:self options:nil];
cell = tvCell;
self.tvCell = nil;
}
UILabel *label;
label = (UILabel *)[cell viewWithTag:5];
label.text = [NSString stringWithFormat:#"Hole #%d", indexPath.row];
return cell;
}
The table view gets created with no errors, but each individual cell contains nothing, but clearly the TVCell.xib has a label with a tag of 5. The only question I have is this. I don't quite understand these steps apple gives here...
Select File’s Owner in the nib document window, open the Identity pane of the inspector, and set the class of File’s Owner to your custom view controller class.
Connect the cell outlet of File’s Owner (now the placeholder instance of your custom subclass) to the table-view cell object in the nib-file document.
Here is where those steps are...
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7
Can someone please explain those steps for a noob like me? I think that is what I messed up on, but I could have done anything wrong.
I don't think TcCell becomes a property of self. Try this instead when the cell queue is empty:
if (cell == nil) {
cell=[[NSBundle mainBundle] loadNibNamed:#"TVCell" owner:self options:nil];
}
It's kind of difficult to explain the steps that you said you don't understand in words. I would suggest looking for tutorials that have images or videos for that. (I did a quick search and there are lots available, but I didn't really want to choose one to direct you to without having read them more closely).
However, I prefer to create custom table view cells this way:
http://www.mobilesce.com/2011/12/27/the-best-way-to-do-custom-reusable-uitableviewcells/
It's slightly different from the way described in the apple docs, but I've seen a lot of people use it and I find it easier.
The NSBundle method loadNibNamed:owner:options: returns an NSArray containing the top level objects in your nib file. Try this:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:#"TVCell" owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}

Several Custom UITableViewCells in one NIB - how to refer to in code?

I'm creating custom UITableViewCells using the approach outlined on this page:
http://icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/
So my cellForRowAtIndexPath code looks a bit like this:
static NSString *CellIdentifier = #"CustomCell";
TableCellWithLogo *cell = (TableCellWithLogo *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"TableCells" owner:nil options:nil];
for (id currentObject in nibObjects)
{
if ([currentObject isKindOfClass:[TableCellWithLogo class]] )
{
cell = (TableCellWithLogo *)currentObject;
}
}
}
cell.customTextLabel.text = [tableArray objectAtIndex:indexPath.row];
return cell;
Now, the table I'm creating looks like a stylized version of the grouped tableview style, with drop-shadows etc. So it needs four possible styles of table (top, center, bottom .. and top+bottom for when only a single cell is showing).
I'm having trouble working out how to put all of these table cells into one nib, and then refer to them. At the moment I have a nib with a UITableViewCell whose class is 'TableCellWithLogo'. This class has all the relevant IBOutlets. If I put several UITableViewCells in the nib (all of TableCellWithLogo class), how would I be able to differentiate between them in code? You can see in the example above I'm finding out the class:
if ([currentObject isKindOfClass:[TableCellWithLogo class]] )
..but all the classes would be the same, so I'm not sure how to differentiate?
Create one empty nib and add table view cells to it. Also, for each of them create a corresponding class and assign the relevant class for each of them (see the screenshot).
Then, in the cellForRowAtIndexPath you'll need to create different cells based on indexPath.row

Custom UITableViewCell erroring

I am trying to build a custom table view using a cell that I built in IB. I am getting a strange error:
<BroadcastViewController 0x4b4f5f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key postText.
Everything is wired up correctly in IB to the cell controller. Not really sure why this is happening.
This is what my cellForRowAtIndexPath looks like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the folder object of interest
Broadcast *messageAtIndex = [self.messages objectAtIndex:indexPath.row] ;
static NSString *CellIdentifier = #"BroadcastTableViewCell";
static NSString *CellNib = #"BroadcastTableViewCell";
BroadcastTableViewCell *cell = (BroadcastTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//ERRORING ON THIS LINE...
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (BroadcastTableViewCell *)[nib objectAtIndex:0];
}
cell.postText.text = messageAtIndex.replyText;
cell.authorName.text = messageAtIndex.postCreatorFirstName;
cell.postDate.text = messageAtIndex.creationDate;
return cell;
}
Anyone seen this kind of error before? Let me know if you need any more information...
What is really strange is that it complains that the class BroadcastViewController is not KVC compliant to postText.
As far as I can see, postText is a label in your cell, so the IBOutlet for this should be in the BroadcastTableViewCell class. So check out where you've linked the postText label in IB. Also, it can be that you had an IBOutlet in your view controller for this label, you've removed it but you forgot to delete the link in IB. Anyway, there somewhere is your problem. The fact that you have the error on that line is just because it's there you load your NIB, it doesn't have anything to do with the cell itself or with the owner.
Might has something to do with dequeueReusableCellWithIdentifier returning an UITableViewCell*.
I normaly do this:
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier...
CustomCell* acell = (CustomCell*)cell;
Set the owner to nil.
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:nil options:nil];
Ok figured it out. The connections in IB were indeed incorrect. I had them linked to the file's owner as opposed to the actual objects. I am going to give this too Stelian because he directed me to check out the nib. Thanks for all your help!

UITableViewCustomCell

i want to use UITableCustomCell in my application but i am invoking the custom cell from another class.
When i use UITableViewCell then i need to add these lines to call tableviewcells from another class
/*
SurahViewController *aBookDetail = [[SurahViewController alloc]
initWithNibName:#"SurahView" bundle:nil];
self.surahViewController = aBookDetail;
[self.navigationController pushViewController:surahViewController animated:YES];
[aBookDetail release];
*/
but when i use UITableViewCustomCell in my application and i want to invoke custom table view cell from another class then there is no idea of what code to use to call that UITableCustomCell from another class.
Thank you in advance.
I'm doing it with the following code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"UILogbookTVCell" owner:self options:nil];
cell = tvCell;
tvCell = nil;
}
Note that tvCell is an UITableView IBOutlet. In the UILogbookTVCell.xib the File's Owner is the UITableViewController implementation, and the UITableViewCell element is connected to the tvCell IBOutlet. Therefore the NSBundle loadNibNamed sets the property tvCell, which can then be set to the local cell and cleared.
This is also the official variant as described in the Table View Programming Guide.