如何在TableView中添加不同的自定义单元格

时间:2015-10-20 23:56:54

标签: objective-c uitableview row cell

我有多个自定义单元格。我将其中的两个子类化。我想用故事板显示具有不同标识符的不同单元格。到目前为止,使用数组我能够显示一个子类单元格,但我有行计数问题和显示多个单元格的技术。我使用延迟来继续添加和更新我的表格。

    NSMutableArray *conv1;
    NSString *l1;
    NSString *l2;
    NSString *l3;
    NSMutableArray *allDialogue;
    NSMutableArray *conv2;
}

@end

@implementation PlotController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    conv1 = [[NSMutableArray alloc] init];
    conv2 = [[NSMutableArray alloc] init];
    allDialogue = [[NSMutableArray alloc] init];
    [allDialogue addObjectsFromArray:conv1];
    [allDialogue addObjectsFromArray:conv2];
    l1 = @"converstaion1";
    l2 = @"converstaion2";
    l3 = @"converstaion3";
    _tableV.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self performSelector:@selector(delay) withObject:nil afterDelay:2.0];
    [self performSelector:@selector(delay2) withObject:nil afterDelay:4.0];
}

tableview config

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MainStoryDialogue *dialogueCell = [tableView dequeueReusableCellWithIdentifier:@"PDialogue"];

    dialogueCell.textHere.text = [allDialogue objectAtIndex:indexPath.row];

    [self.tableV beginUpdates];
    [self.tableV endUpdates];

    return dialogueCell;
}

第二个问题是行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [allDialogue count];
}

2 个答案:

答案 0 :(得分:1)

我认为,为了能够在表格视图中显示多个自定义单元格,首先,在故事板中,您需要单击tableView,然后在属性检查器下,您将看到一个名为Prototype Cells的字段。选择您要使用的不同单元格的数量,根据需要自定义它们,为每个单元格创建一个TableViewCell类,进行所有适当的连接,并为每个单元格设置唯一标识符。完成此操作后,在您的cellForRowAtIndexPath方法中(基于任何条件),您可以使用您创建的自定义单元格类和相应的单元格标识符来实例化您要为给定行创建的特定单元格。

所以例如:

if (indexpath.row == 1) {
    MainStoryDialogue *dialogueCell = [tableViewdequeueReusableCellWithIdentifier:@"PDialogue"];

//do all appropriate things if you have this cell
} else {
    CustomCell2 *customCell2 = [tableView dequeueReusableCellWithIdentifier:@"customCell2"];

//do all appropriate things if you had this cell
}

答案 1 :(得分:1)

我在这里找到答案UITableView With Multiple Sections 谢谢你的回复

相关问题