如何在UITableView中创建3种不同类型的自定义单元格?

时间:2017-02-07 04:41:31

标签: ios objective-c

我有3种不同类型的自定义单元格,如何使用不同类型的自定义单元格创建teble视图。

2 个答案:

答案 0 :(得分:1)

假设您有3个自定义单元格。您可以通过这种方式使用它们:

CustomCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:[CustomCell1 reuseIdentifier]];
CustomCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:[CustomCell2 reuseIdentifier]];
CustomCell3 *cell3 = [tableView dequeueReusableCellWithIdentifier:[CustomCell3 reuseIdentifier]];

    if indexPath == 0 {
        if (cell1 == nil) {
            cell1 = [[CustomCell1 alloc] initWithOwner:self];
        }
         return cell1;

    }
    else if indexPath == 1 {
        if (cell2 == nil) {
            cell2 = [[CustomCell2 alloc] initWithOwner:self];
        }
       return cell2;

    }
    else {
        if (cell3 == nil) {
            cell3 = [[CustomCell3 alloc] initWithOwner:self];
        }
       return cell3;

    }

答案 1 :(得分:1)

创建3个自定义单元格。 例如: -

static NSString *strIdentifier1 = @"cellHomeText";
static NSString *strIdentifier2 = @"cellHomeImage";
static NSString *strIdentifier3 = @"cellHomeImageWithText";

HomeViewTextCell *cell1 = [tableView dequeueReusableCellWithIdentifier:strIdentifier1];
HomeViewImageCell *cell2 = [tableView dequeueReusableCellWithIdentifier:strIdentifier2];
HomeViewImageWithTextCell *cell3 = [tableView dequeueReusableCellWithIdentifier:strIdentifier3];