这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //custom code
    [self.table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    //add table
    ....
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //a method that add context for cell, return void
    [self cellView:cell withInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"cell: %@ -------- indexpath: %d", cell, indexPath.row);
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

问题是滚动时,复选标记会重新出现在另一个单元格上。
这是NSLog的结果:
cell: <UITableViewCell: 0x16f37a60; frame = (0 0; 320 50); text = ' Hoàng Anh'; autoresize = W; layer = <CALayer: 0x16f33f80>> -------- indexpath: 0
cell: <UITableViewCell: 0x16f37a60; frame = (0 450; 320 50); text = '84'; autoresize = W; layer = <CALayer: 0x16f33f80>> -------- indexpath: 9
cell: <UITableViewCell: 0x16f37a60; frame = (0 900; 320 50); text = 'Ân Thi'; autoresize = W; layer = <CALayer: 0x16f33f80>> -------- indexpath: 18

单元1-10-20或2-11-21的相同问题...

如您所见,单元格的地址没有改变。这就是复选标记重新出现的原因。

我如何摆脱这个问题?

谢谢。

最佳答案

@property(nonatomic, strong)NSMutableDictionary *selectionDetails;


-(NSMutableDictionary)selectionDetails{
    if(!_selectionDetails)
        _selectionDetails = [[NSMutableDictionary alloc]init];
    return _selectionDetails;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    **if([self.selectionDetails objectForKey:@"accessoryType"]){
       NSString *status = [self.selectionDetails objectForKey:@"accessoryType"];
       if([status isEqualToString:@"checked"])
          [self.selectionDetails setObject:@"checked" forKey:@"accessoryType"];
       else
          [self.selectionDetails setObject:@"none" forKey:@"accessoryType"]
    }else{
        [self.selectionDetails setObject:@"none" forKey:@"accessoryType"]
    }**
    //a method that add context for cell, return void
    [self cellView:cell withInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"cell: %@ -------- indexpath: %d", cell, indexPath.row);
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        **[self.selectionDetails setObject:@"checked" forKey:@"accessoryType"];**
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        **[self.selectionDetails setObject:@"none" forKey:@"accessoryType"];**
    }
}

关于ios - 触摸单元格时更改UITableViewCell的附件类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21722720/

10-15 04:49
查看更多