I’m trying to use a custom UITableViewCell, and I’ve placed it in the same nib file as the UITableView controller. For this, the files are: NTItems.h, NTItems.m and NTItems.xib.
I have defined the cell in the header file:
IBOutlet UITableViewCell *cellview;
and I’ve correctly applied the property: nonatomic, retain so it’s there:
@property (nonatomic, retain) IBOutlet UITableViewCell *cellview;
In the m file – I’ve synthesized the variable device and am using this to get the custom cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cellview";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = self.cellview;
}
Product *aProduct = [appDelegate.products objectAtIndex:indexPath.row];
name.text = aProduct.name;
upc.text = [NSString stringWithFormat:@"UPC%@",aProduct.upc];
price.text = aProduct.pid;
return cell;
}
However, when I load the table, I get this horrible mess:
alt text http://dl.dropbox.com/u/1545603/tablecellissue.png
There should be more than 1 cell showing data as well. It appears that only the last data is showing up right now.
You can’t reuse a single cell from an outlet like this. Think about it: you’re returning the same cell for every call to
tableView:cellForRowAtIndexPath:. It’s your job to ask the tableView to dequeue a cell if possible, and create a new one each time it isn’t.Instead, store your custom cell in a separate nib and read that nib within your
if (cell == nil) { }code when dequeue fails.The nib file that contains the custom cell should have its File’s Owner NSObject and it should contain only the cell’s nib (no other objects).
I use this function to load the nib:
Then in my
tableView:cellForRowAtIndexPath:I have:(I use the same nib name as my cell reuse identifier.)