jac300
jac300

Reputation: 5222

Difficulty Accessing UITableViewCell Created in Interface Builder .xib File

In my UITableView, for the last row of the last section of the table, I load a special UITableViewCell that is different from all the others on the table. I created the cell in my .xib file and gave it the reuse identifier "endCell." I would think that I could just do the following to access my cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if ((indexPath.section == [sections count] - 1) && indexPath.row == [[sections objectAtIndex:indexPath.section] count])) {

    return [tableView dequeueReusableCellWithIdentifier:@"endCell"];

} //more code for other cells...

I have set the cell identifier in the interface builder. However, running this code causes a crash with an error:

"Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:],..."

After researching this error, the only solution I could find was to get a pointer to the .xib cell by accessing it through an array that holds the objects contained inside the .xib. So I tried the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if ((indexPath.section == [sections count] - 1) && (indexPath.row == [[sections objectAtIndex:indexPath.section] count]) ) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"endCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]init];
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PLNewConversationViewController" owner:self options:nil];
            for (id xibItem in topLevelObjects) {
                if ([xibItem isKindOfClass:[UITableViewCell class]]){
                    UITableViewCell *tableViewCell = (UITableViewCell *)xibItem;
                    if ([tableViewCell.reuseIdentifier isEqualToString:@"endCell"]) {
                        cell = xibItem;   
                    }
                }
            }
        }

    return  cell;
} //more code for other cells...

This results in very strange behavior - it's not even obvious to me what it is doing - the last cell is never shown, and it appears to be scrolling endlessly because when it gets to the bottom of the table, it automatically jumps back up to the top of the table. Sometimes there is a crash with varying errors.

I have managed to work around this by hooking up my cell to the necessary class as an outlet and just returning the cell as "return self.endCell..." but I feel like I should be able to access the cell without doing this.

Can anyone see what I am doing wrong?

Upvotes: 2

Views: 935

Answers (1)

Martin R
Martin R

Reputation: 539965

The easiest way to load custom table view cells from a nib file is to create them in a separate xib file (containing only that cell) and registering the generated nib file with

[self.tableView registerNib:[UINib nibWithNibName:@"YourNibFile" bundle:nil] forCellReuseIdentifier:@"YourReuseIdentifier"];

for example in viewDidLoad of the table view controller.

[tableView dequeueReusableCellWithIdentifier:@"YourReuseIdentifier"]

will then instantiate cells from that nib file if necessary.

Upvotes: 3

Related Questions