在UITableView中添加多个自定义单元格 [英] Adding multiple custom cells in UITableView

查看:128
本文介绍了在UITableView中添加多个自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然这是最常问的问题之一,但我找不到一个全面的答案。我需要在UITableView中有自定义单元格。一些包含标签或文本字段,一些包含图像和按钮。我为每种类型的细胞制作了单独的类。我正在使用具有多个部分的GroupStyle表。现在我在cellForIndexPath中添加了switch,case为section,if-else为section in section:

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;

我也必须在最后返回单元格,因为由于代码块内的单元格的定义,单元格变得无法辨认。为了解决这个问题,我在顶部声明了id的单元格。但我知道这不是正确的方法。如何解决此声明并访问多种类型单元格的问题?

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?

目前有4-5行适合一个屏幕而不需要滚动。所以,我不是在重复使用细胞。但是在编辑时会挤出更多行。在另一个表中,有更多行可以滚动屏幕。这意味着我必须重复使用细胞。所以,我的问题的第二部分是;如何重复使用多个自定义单元格?

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?

推荐答案

要回答您的第一个问题,您也可以返回没有因为你没有很好的回报价值。如果遇到这种情况,将抛出异常;就像现在一样,它可能会在框架代码中的某处给你一个EXC_BAD_ACCESS。

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.

要回答你的第二个问题,每个类型的单元格应该有一个唯一的reuseIdentifier。例如,所有CellA都可以具有@CellA的reuseIdentifier。那么你将完全重用它们,就像你所有单元格相同的情况一样:当你需要一个CellA调用 [tableView dequeueReusableCellWithIdentifier:@CellA] 时,你需要一个CellB调用 [tableView dequeueReusableCellWithIdentifier:@CellB] ,依此类推。例如,

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,

    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;

这篇关于在UITableView中添加多个自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆