Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Reusing IB created UITableViewCells

Hi,

I have created an UITableViewCell in IB which contains two UILabels. In the controller I have created Outlets for the cell as well as the labels. Now I want to reuse this cell within the table view (see code below). Unfortunatly it does not work as expected. The cell appears only once and two empty cells above it. What's wrong with my code? I guess I missunderstand the dequeueing mechanism.
Does anyone have a hint?

Best regards,
Michael

* Code *
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xxx"];

if(cell == nil) {
cell = myCell // myCell is an Outlet - connected in IB
}

// label1 and label2 are also Outlets
label1.text = [NSString stringWithFormat:@"%d", indexPath.row];
label2.text = [NSString stringWithFormat:@"%d", indexPath.section];

return cell;
}

Posted on Dec 2, 2008 2:26 PM

Reply
3 replies

Dec 2, 2008 6:48 PM in response to Default

1) Put the UITableViewCell in a nib by itself.
2) Each time a new cell is needed load the nib, each new cell must be unique.


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LabpleCellID"];
if(nil == cell) {
[[NSBundle mainBundle] loadNibNamed:@"LabpleCellID" owner:self options:nil];
cell = [cellFactory retain]; // get the object loadNibNamed has just created into cellFactory
cellFactory = nil; // make sure this can't be re-used accidentally
}

Where "LabpleCellID" is the cell ID in IB.

Dec 3, 2008 4:28 AM in response to Default

!) I forgot to mention the IBOutlet:
IBOutlet UITableViewCell *cellFactory;

and setting the Files Owner's class to the class that is the UITableView datasource.

2) Set tags for each UILabel in IB, in ths example I set the tags as 1 & 2:
((UILabel *)[cell viewWithTag:1]).text = string1; 
((UILabel *)[cell viewWithTag:2]).text = string2;

Reusing IB created UITableViewCells

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.