WaJ*_*yaz 7 iphone uitableview custom-cell
虽然这是最常见的问题之一,但我找不到一个全面的答案.我需要在UITableView中有自定义单元格.一些包含标签或文本字段,一些包含图像和按钮.我为每种类型的细胞制作了单独的类.我正在使用具有多个部分的GroupStyle表.现在我在cellForIndexPath中添加单元格,其中section为switch-case,if-else为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;
Run Code Online (Sandbox Code Playgroud)
我也必须在最后返回单元格,因为由于代码块内部的单元格的定义,单元格变得无法识别.为了解决这个问题,我在顶部声明了id的单元格.但我知道这不是正确的方法.如何解决此声明和访问多种类型单元格的问题?
目前有4-5行适合一个屏幕而不需要滚动.所以,我不是在重复使用细胞.但是在编辑时会挤出更多行.在另一个表中,有更多行可以滚动屏幕.这意味着我必须重复使用细胞.所以,我的问题的第二部分是; 如何重用多个自定义单元格?
Ano*_*mie 11
要回答你的第一个问题,你也可以回来,nil因为你没有什么好回报.如果遇到这种情况,将抛出异常; 就像现在一样,它可能会在框架代码中的某处给你一个EXC_BAD_ACCESS.
要回答第二个问题,每种类型的单元格都应该有一个唯一的reuseIdentifier.例如,所有CellA都可以具有@"CellA"的reuseIdentifier.然后,您将完全重用它们,就像所有单元格相同的情况一样:当您需要[tableView dequeueReusableCellWithIdentifier:@"CellA"]CellA调用时,何时需要CellB调用[tableView dequeueReusableCellWithIdentifier:@"CellB"],等等.例如,
case 0:
if (indexPath.row == 0) {
CellA *cell = [tableView dequeueReusableCellWithIdentifier:@"CellA"];
if (!cell) {
cell = [[[CellA alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellA"] autorelease];
}
// configure cell
return cell;
}
else if (indexPath.row == 1) {
CellB *cell = [tableView dequeueReusableCellWithIdentifier:@"CellB"];
if (!cell) {
cell = [[[CellB alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellB"] autorelease];
}
// configure cell
return cell;
}
break;
case 1:
if (indexPath.row == 0) {
CellC *cell = [tableView dequeueReusableCellWithIdentifier:@"CellC"];
if (!cell) {
cell = [[[CellC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellC"] autorelease];
}
// configure cell
return cell;
}
break;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16433 次 |
| 最近记录: |