在触摸单元格时更改 UITableViewCell 的访问类型



这是我的代码:

- (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"];**
    }
}

试试这个,

- (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];
    }
    NSDictionary *taxiDictionary = [self.arrayTaxiList objectAtIndex:indexPath.row];
    if ([[taxiDictionary valueForKey:@"accessoryType"] isEqualToString:@"none"]) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    //a method that add context for cell, return void
    [self cellView:cell withInfo:taxiDictionary];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"cell: %@ -------- indexpath: %d", cell, indexPath.row);
    NSMutableDictionary *taxiDictionary = [NSMutableDictionary dictionaryWithDictionary:[self.arrayTaxiList objectAtIndex:indexPath.row]];
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        [taxiDictionary setValue:@"checked" forKey:@"accessoryType"];
    } else {
        [taxiDictionary setValue:@"none" forKey:@"accessoryType"]
    }
    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [tableView endUpdates];
}

对不起,伙计们,我所描述的问题是由于我的愚蠢而发生的。

这一切都发生在这行代码中

//a method that add context for cell, return void
[self cellView:cell withInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
-(void)cellView:(UITableViewCell *)cell withInfo:(NSDictionary *)info {}

此方法接收UITableViewCell并编辑其上下文。因此,这就是为什么上面的NSLog返回内存中具有相同地址的UITableViewCell

我把它改成了

cell = [self cellViewWithInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
-(UITableViewCell *)cellViewWithInfo:(NSDictionary *)info {}

现在它像它应该的那样工作。

谢谢你们的回复。

最新更新