【问题标题】:How to add different Custom Cells in TableView [duplicate]如何在 TableView 中添加不同的自定义单元格 [重复]
【发布时间】:2015-10-20 23:56:53
【问题描述】:

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

    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];
}

表格视图配置

- (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];
}

【问题讨论】:

  • 像我说的那样添加多个自定义单元格
  • 顺便说一句,cellForRowAtIndexPath 表视图控制器生命周期方法中的开始和结束更新调用什么都不做。

标签: objective-c uitableview row cell


【解决方案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
}

【讨论】:

  • 感谢您的反馈,但它没有用,直到行更新我猜测它的 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {返回[所有对话计数];我可以用什么替换它
  • 我只是通过延迟添加到数组来更新
  • 好的,所以当您更新并向数组添加内容时,请调用 [self.tableView reloadData];其中 tableView 是对应用中 UITableView 的引用。
  • 延迟的目的是什么?
  • 在我的延迟中我调用[self.tableView reloadData] 并添加到数组[conv1 addObject:@"addition"]; 延迟是看到它在特定时间自动更新
猜你喜欢
  • 2012-12-06
  • 2013-07-25
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
相关资源
最近更新 更多