I have successfully used custom UITableViewCells using separate classes, but I keep thinking there is an easier way so I have tried the following. The table builds, but the cells are blank (so presumably my custom cell is not displaying correctly). Any ideas appreciated.
My files are:
@interface RemediesPopUp : UIViewController <UITableViewDataSource, UITableViewDelegate>{
UITableView *remediesDataTable;
UITableViewCell *remediesTableCell; // ** custom cell declared here **
NSArray *remediesArray;
}
The table controls are:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
remediesTableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (remediesTableCell == nil) {
remediesTableCell = [[[UITableViewCell alloc] init] autorelease];
}
// Configure the cell.
return remediesTableCell;
}
I dragged a custom cell object into the XIB file pane (so I have the View and a separate custom cell displayed). I connected the custom cell to File Manager. To test, I pasted a couple of (static) labels onto the custom cell so I could see if it was loading.
The page and table loads just fine, but the cells are blank (white).
Any ideas?
First off, make sure that your custom cell is a custom subclass (subclass of
UITableViewCell) and has its own nib file (for example, don’t integrate it with your table view controller).In the nib, select the identity inspector and ensure that your cell’s class is
CustomCell(or whatever you called it). Also make sure that under the attributes inspector you set itsIdentifierto “CustomCellIdentifier” or similar.In your table view data source, your
tableView:cellForRowAtIndexPathmethod should contain code similar to this:This ensures that your custom cell is loaded directly from its own nib, allowing you to visually lay out any views or labels. The text for these labels can then be set once the appropriate cell has been dequeued/created.
In your code, your ‘custom’ cell is just a plain
UITableViewCell. To customise the cell using interface builder, you need to create your own custom subclass of this.EDIT
For a custom cell that uses only a nib (and not a custom subclass), change the above code example to cast the cell to a plain
UITableViewCell, i.e. changeCustomCelltoUITableViewCell. In Interface Builder assign each of your cell’s custom views (labels etc) with a unique tag.With that in place, the code should look something like this: