在 UITableView 中添加多个自定义单元格
虽然这是最常被问到的问题之一,但我找不到一个全面的答案。我需要在 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 行适合一个屏幕并且不需要滚动。所以,我不会重复使用细胞。但编辑时会挤入更多行。在另一个表中,有更多的行可以滚动屏幕。这意味着我必须重复使用细胞。所以,我的问题的第二部分是;如何重用多个自定义单元格?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要回答你的第一个问题,你也可以返回 nil,因为你没有好的返回值。如果遇到这种情况,就会抛出异常;就像现在一样,它可能会在框架代码内的某个位置给你一个 EXC_BAD_ACCESS 。
要回答第二个问题,每种类型的单元都应该有一个唯一的reuseIdentifier。例如,所有 CellA 都可以具有@“CellA”的重用标识符。然后,您将完全重用它们,就像在所有单元格都相同的情况下一样:当您需要 CellA 调用时
[tableView dequeueReusableCellWithIdentifier:@"CellA"]
时,当您需要 CellB 调用时 <代码>[tableView dequeueReusableCellWithIdentifier:@"CellB"],等等。例如,To answer your first question, you may as well return
nil
as you have no good value to return. If it ever hits this case, an exception will be thrown; as it is now, it's likely to give you an EXC_BAD_ACCESS somewhere inside the framework code.To answer your second question, each type of cell should have a unique reuseIdentifier. For example, all the CellA's could have a reuseIdentifier of @"CellA". Then you would reuse them exactly as you would in the case that all the cells were the same: when you need a CellA call
[tableView dequeueReusableCellWithIdentifier:@"CellA"]
, when you need a CellB call[tableView dequeueReusableCellWithIdentifier:@"CellB"]
, and so on. For example,添加 UITableView 到 UIView。添加自定义单元格,关联自定义单元格类并实现委托 -UITableviewDelegate 和 UITableViewDataSource 。
情况1:tableview上的两个自定义单元格
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
情况2:交替显示自定义单元格(即使用uisegmentcontrol)
情况3:交替显示自定义单元格(即奇偶)
Add UITableView To UIView.Add custom cells, associate custom cell classes and implement delegates -UITableviewDelegate and UITableViewDataSource .
case 1: Two custom cells on tableview
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
case 2: alternate display of custom cells (i.e. using uisegmentcontrol)
case 3: Alternate custom cells (i.e., odd even)