The Man
The Man

Reputation: 1462

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...

  1. 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.

  2. 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.

Upvotes: 0

Views: 1920

Answers (3)

jonkroll
jonkroll

Reputation: 15722

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];
}

Upvotes: 0

Darren
Darren

Reputation: 10129

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.

Upvotes: 0

user1258240
user1258240

Reputation: 855

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];
}

Upvotes: 1

Related Questions