ios - 触摸单元格时更改UITableViewCell的附件类型

标签 ios objective-c uitableview

这是我的代码:

- (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/

相关文章:

ios - 插入 View Controller 时防止键盘关闭

ios - 在目标 View Controller 上设置委托(delegate)

ios - 如何在 iOS 7 和 iOS 8 上正确自定义 UITabBar 和 UITabBarItem?

iOS在表格中显示数组中的数据,最后一个索引除外

ios - 如何在按下按钮时发布 Alamofire JSON 请求?

ios - 如何以编程方式将代理添加到 NSURLSession

objective-c - 我需要调用 [super viewDidUnload] 吗?

swift - 即使未调用函数,Firebase也会观察新添加的数据

uitableview - 将图像添加到单元格中

iphone - 在 iPhone 中的 LinkedIn 集成中共享视频 URL