Reputation: 8674
I'm not using a storyboard and it's iOS 6.
I get what to do when all the cells are the same type but I require a grouped table view with multiple types of cells. Just as an example, let's say the first cell needs to be UITableViewCellStyleValue1
, the second cell needs to be UITableViewCellStyleSubtitle
, and the third cell is a custom cell (a UITableViewCell
subclass that has a xib so it can be used with registerNib:forCellReuseIdentifier:
.
Mostly I'm unsure of the best way to structure tableView:cellForRowAtIndexPath:
. But I'm not sure if I should register the custom cell or all of them.
What is the best way to do this?
Upvotes: 2
Views: 3501
Reputation: 2170
You are on the right path. Since your custom cell is being used in other places a xib is a great place to load it up from. As far as the implementation you could do something like this.
Assuming your tableview is 'static' and has three cells, you could register your custom nib in viewDidLoad
:
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *customCellNib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
[self.tableView registerNib:customCellNib forCellReuseIdentifier:@"CustomIdentifier"]
}
Then in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if(indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier1"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:@"CellIdentifier1"];
}
}
/* Cell 2 ommited for brevity */
else if(indexPath.row == 2) {
//Just to demonstrate the tableview is returning the correct type of cell from the XIB
CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:@"CustomIdentifier"];
cell = customCell;
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
Finally in IB for the Xib, set the proper Identifier
for the cell.
Update
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == 0) {
cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];
}
else {
//custom cell here
//cell.textfield.text = @"blah blah";
}
}
A configure cell method is somewhat of a convention for tableview cells put in place primarily with the NSFetchedResultsController (and its delegate where its used)
It's just a convenient way to reset reused cells with the proper content, and make cellForRowAtIndexPath:
easier to read. I even make multiple version of configureCell like configureCustomCell1:atIndexPath
to increase readability even more.
Hope this helps!
Upvotes: 3
Reputation: 23223
If you are using a static table you won't need reuse identifiers or anything else because no cell will ever get re-used. Just set the table to be "Static Cells" of style "Grouped" and set the number of sections you want. You can click on the "Table View Section" and set how many rows are in the section (and set any header/footer you might want).
Then configure the cells however you would like. You can either choose a default cell or make the cell "Custom" and drag UI components from the Object Library. You can set up IBOutlets
or IBActions
to components in a static table cells just like you would for components in a normal view.
Upvotes: 0