一个UITableView中有多个UITableViewCell类? [英] Multiple UITableViewCell Classes in One UITableView?

查看:107
本文介绍了一个UITableView中有多个UITableViewCell类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在组装一个TableView,我需要在同一个表中使用多个单元类。

I am putting together a TableView and I need to have multiple cell classes used in the same table.

所以例如我如何使用多个单元类在我的 cellForRowAtIndexPath 方法?

So for example how would I use more than one cell class in my cellForRowAtIndexPath method?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...

    return cell;
}

我知道我可以简单地替换 UITableViewCell 使用我的自定义类并使用if语句根据索引来调整类,但是不是有点乱,什么是合理的,可能是最聪明的方法呢?

I know I could simply replace UITableViewCell with my custom class and use if statements to adjust the class depending on the index, but isn't that a bit messy, what would be a reasonable, and possibly the most intelligent way of doing this?

推荐答案

当然可以。只需创建自定义单元格的新实例并为它们提供一个新的CellId。

Sure can. Just create new instances of your custom cells and give them a new CellId.

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   if (condition1)
    {
        static NSString *CellIdentifier1 = @"Cell1";
        UITableViewCell *cell = 
         [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

        // TODO: if cell is null, create new cell of desired type.  
        // This is where you    create your custom cell that is 
        // derived form UITableViewCell.
    }
    else
    {
        static NSString *CellIdentifier2 = @"Cell2";
        UITableViewCell *cell = 
         [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

        //TODO: if cell is null, create new cell of desired type

    }
    // Configure the cell...

    return cell;
}

这篇关于一个UITableView中有多个UITableViewCell类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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