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

时间:2014-02-12 08:44:46

标签: 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 ...

相同的问题

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

如何摆脱这个问题?

感谢。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

试试这个,

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

答案 2 :(得分:0)

抱歉,伙计,我被描述的问题是因为我的愚蠢而发生的。

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

//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 {}

现在它的工作方式应该如此。

谢谢各位回复。