尽管这是最常问的问题之一,但我找不到一个全面的答案。我需要在UITableView中定制单元格。一些包含标签或文本字段,一些包含图像和按钮。我已经为每种类型的单元制作了单独的类。我正在使用具有多个部分的GroupStyle表。现在我将与开关的情况下cellForIndexPath细胞部分,如果其他的行节:在UITableView中添加多个自定义单元格
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;
我必须在最后返回单元,以及因为由于细胞的定义里面的代码块,细胞变得无法辨认。为了解决这个问题,我在上面声明了带有id的cell。但是我知道这不是正确的方法。我该如何解决多种类型单元格的声明和访问问题?
现在有4-5行适合一个屏幕,不需要滚动。所以,我不重复使用单元格。但编辑时会挤入更多的行。而在另一个表格中,还有更多行可以滚动屏幕。这意味着我必须重用单元格。所以,我的问题的第二部分是;我如何重用多个自定义单元格?
你并不真正需要的switch-case – Markinson 2015-07-02 23:27:48