0

所以我有通过 builder 定义的自定义表格单元格并通过 nib 加载(它有一个 idexPath 的属性),它有 2 个按钮。我想显示这些按钮状态动态变化的表格。IE 第一个单元格 - 都启用,第二个单元格 - 两个按钮都禁用,第三个 - 第一个 btn 启用和第二个 btn 禁用等等。
现在,如果我使用 1 个重用标识符,所有单元格将看起来与我不想要的相同。我希望每个单元格都有自己的视图,这意味着每个单元格都有唯一的重用 ID。
但是如何达到这一点?如果我将创建一些独特的 cellId 在

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

这将是一个字符串,那么我无法在对象的含义上创建相同的字符串。我可以创建具有相同文本的字符串,但这将是另一个对象,因此我无法通过reuseId 再次获取先前创建的具有此类cellId 的单元格。所以我不能改变一个单元格的按钮状态,然后用

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationNone];

但只有[tableView reloadData];意志起作用。

4

1 回答 1

1

我有一种感觉,当您第一次使用initWithStyle:reuseIdentifier:创建单元格时,您只是在设置单元格按钮的状态。这是做事的错误方式。您需要在每次调用cellForRowAtIndexPath时为您的单元格设置状态,无论它们是否被重新使用。在您的情况下,如果每个单元格具有相同的 UI(两个按钮),那么它们都应该共享一个重用标识符。您的数据源应该负责维护单元格的状态,而不是UITableViewCell 对象。

这之间的区别是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     myCustomCell *cell = (myCustomCell *)[myTable dequeueReusableCellWithIdentifier:@"myCellIdentifier"];
     if (cell == nil) {
       // Load cell from nib here and set the cell's button states based on indexPath
     }
    return cell;
}

还有这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     myCustomCell *cell = (myCustomCell *)[myTable dequeueReusableCellWithIdentifier:@"myCellIdentifier"];
     if (cell == nil) {
       // Load cell from nib here
     }
    // set the cell's button states based on indexPath here
    return cell;
}
于 2012-07-03T23:09:38.023 回答