UITableViewCell和UITableView重复选择其他单元格?

时间:2011-06-02 07:44:52

标签: iphone objective-c uitableview

我正在 UITableView 中工作并尝试让用户使用此代码选择单元格

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = [indexPath row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell != nil)
    {
        if(cell.accessoryType == UITableViewCellAccessoryNone)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
}

但问题是,如果您选择单元格编号1,单元格数超过一页,则每个页面中的行编号1也将被选中。

3 个答案:

答案 0 :(得分:8)

static NSString * cellIdentifier = @"CellIdentifier" 

在这个地方使用

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i",indexPath.row];

答案 1 :(得分:5)

很好的答案,你真的帮助我走上正轨。

我帮助别人的2美分:

我正在使用一个分组表,所以我把它改了一点就可以了:

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i-%i", indexPath.section, indexPath.row];

希望这有助于其他人。

答案 2 :(得分:2)

使用此方法将全局变量一个类传递给其他类或一个视图控制器传递给另一个视图控制器。

globalString=[Arrayname objectAtIndex:indexPath.row];

将此代码写入tableviewdidselect委托方法。