Though this is one of the most asked question but i could not find one comprehensive answer. I need to have custom cells in UITableView. Some containing labels or text fields and some with images and buttons. I have made separate classes for each type of cell. I am using GroupStyle table with multiple sections. Right now I am adding cells in cellForIndexPath with switch-case for section and if-else for rows in section:
id cell;
switch(indexPath.section) {
case 0:
if(indexPath.row==0) {
CellA *cell = [[[CellA alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
//configure cell
return cell;
}
else if(indexPath.row==1) {
CellB *cell = [[[CellB alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
//configure cell
return cell;
}
break;
case 1:
if(indexPath.row==0) {
CellC *cell = [[[CellC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Celld%",indexPath.row]] autorelease];
//configure cell
return cell;
}
break;
default:
break;
}
return cell;
I have to return cell at the end as well because due to definition of cells inside code blocks, cell becomes unrecognizable. To solve it, i declared cell with id on top.But i know this is not the right way. How can I resolve this declare and access issue of multiple types of cells?
There are 4-5 rows at the moment which fit one screen and do not need scrolling. So, I am not reusing cells. But more rows will squeeze in while editing. And in another table, there are more rows which can scroll of the screen. This means I must reuse cells. So, second part of my question is; how can I reuse multiple custom cells?