虽然这是被问到最多的问题之一,但我找不到一个全面的答案。我需要在 UITableView 中有自定义单元格。有些包含标签或文本字段,有些包含图像和按钮。我为每种类型的细胞制作了单独的类。我正在使用具有多个部分的 GroupStyle 表。现在我在 cellForIndexPath 中添加单元格,其中部分的 switch-case 和部分中的行的 if-else:
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 的单元格。但我知道这不是正确的方法。如何解决多种单元格的声明和访问问题?
目前有 4-5 行适合一个屏幕,不需要滚动。所以,我没有重复使用细胞。但是在编辑时会挤进更多的行。而在另一个表中,有更多的行可以滚动屏幕。这意味着我必须重复使用单元格。所以,我的问题的第二部分是;如何重用多个自定义单元格?