1

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

if (cell.accessoryType == UITableViewCellAccessoryNone) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

...修改仅选定行的每 6 个单元格的“accessoryType”。我错过了什么?

谢谢

更新:这是单元格创建代码...

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

    static NSString *TC = @"TC";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TC];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PlayerTableViewCell] autorelease];
    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [NSString stringWithFormat:@"Person %d", row+1];

    return cell;
}

基于以下标记答案的我的解决方案是......

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

    // EVERY row gets its one Identifier
    NSString *TC = [NSString stringWithFormat: @"TC%d", indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TC];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TC] autorelease];
    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [NSString stringWithFormat:@"Person %d", row+1];

    return cell;
}

如果有更好的方法,我会全力以赴。如果我们可以根据某一天传递的 NSIndexPath 更改 SPECIFIC Cell 那就太好了(至少这对我来说似乎更直观)。

4

2 回答 2

1

我通过使用基于跟踪所选行的变量在 cellForRowAtIndexPath 中设置附件类型来使其工作。然后在 didSelectRowAtIndexPath 中,我只需取消选择行,更新变量并重新加载表。

代码示例:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];

    if ([_cells indexOfObjectIdenticalTo:_selectedCell] == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    _selectedCell = [_cells objectAtIndex:indexPath.row];
    [tableView reloadData];
}
于 2010-05-12T22:58:16.253 回答
0

是的,刚刚想通了。这些是相同的单元格(下面的日志是 NSLog(@"%d %@", row, cell))。

2010-04-15 12:43:40.722 ...[37343:207] 0   <MyListCell: 0x124bee0; baseClass = UITableViewCell; frame = (0 0; 320 44); autoresize = RM+TM; layer = <CALayer: 0x124c3a0>>

2010-04-15 12:43:47.827 ...[37343:207] 10   <MyListCell: 0x124bee0; baseClass = UITableViewCell; frame = (0 31; 340 44); autoresize = W; layer = <CALayer: 0x124c3a0>>

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: PlayerTableViewCell];

dequeueReusableCellWithIdentifier 使用已经创建的单元格,如果前一个单元格不在屏幕上。

所以,你应该避免使用“dequeueReusableCellWithIdentifier”——创建一个字典 NSIndexPath -> UITableViewCell 并使用它来代替 dequeueReusableCellWithIdentifier。

添加: 您还可以检查 (rowNumber+1) 是否存在于 winsNumbers 数组中,并在 tableView:cellForRowAtIndexPath: 方法中替换附件。这也应该工作

于 2010-04-15T08:48:33.580 回答