0

When i add a label to a cell using:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell addSubView:someLabel];
}

It adds the label to multple cells, because it adds the label to a cached cell. So every cell in the tableView thats uses a cached cell, will display my label.

Does somebody knows a way around? Ton

4

5 回答 5

2

给出唯一标识符名称,例如:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    static NSString *CellIdentifier = NSLocalizedString(@"Cell",@"");
…
}

与其给出常量标识符名称,不如给出您自己的唯一标识符名称,例如:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    static NSString *CellIdentifier = [self getuniqueID];
...
}

这将解决您的问题。

于 2010-03-01T06:02:39.397 回答
1

如果您的单元格是 UITableViewCell 的子类,您可以覆盖- (void)prepareForReuse在返回单元格之前调用哪个单元格- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier

这是为您的单元格提供“干净的石板”的方法,但仅限于即将被重用的缓存单元格。

于 2010-03-01T03:50:45.010 回答
1

为什么不只是简化事情并默认隐藏标签并将其包含在所有单元格中。

当您点击单元格时,而不是添加视图 - 只显示存在但隐藏的视图。

完成后,隐藏标签。

于 2010-03-01T06:11:00.180 回答
0

您需要跟踪每个单元格当前是否应用了标签。检查它是否应该在cellForRowAtIndexPath:那里,如果是,则在必要时添加它,否则在必要时将其删除。

于 2010-02-28T21:28:59.447 回答
0

我有时使用此代码:(当单元格!= nil 时)

NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UIView *subview in subviews) {
    [subview removeFromSuperview];
}
[subviews release];

它删除缓存单元的所有子视图。

于 2010-09-10T10:18:35.240 回答