Objective c 当取消选择自定义单元格时,图像不会';不要在iOS上改变

Objective c 当取消选择自定义单元格时,图像不会';不要在iOS上改变,objective-c,iphone,uitableview,checkbox,uiimage,Objective C,Iphone,Uitableview,Checkbox,Uiimage,我正在使用具有自定义单元格的UITableView。自定义单元格只有一个标签和一个UIImage,将使用UIImage作为复选框 因此,问题是,一旦我单击一行,复选框图像将变为选中标记png图像(这是确定的),但一旦我再次单击同一行,它不会取消选中该行,函数diddescrowatinexpath将运行,我设置了一些nslog来检查,但是图像没有更改为取消选中框,背景色也没有更改 我觉得是因为tableview没有刷新 有趣的是,如果我不使用自定义单元格,而只使用accessoryType to

我正在使用具有自定义单元格的
UITableView
。自定义单元格只有一个标签和一个
UIImage
,将使用
UIImage
作为复选框

因此,问题是,一旦我单击一行,复选框图像将变为选中标记png图像(这是确定的),但一旦我再次单击同一行,它不会取消选中该行,函数
diddescrowatinexpath
将运行,我设置了一些
nslog
来检查,但是图像没有更改为取消选中框,背景色也没有更改

我觉得是因为tableview没有刷新

有趣的是,如果我不使用自定义单元格,而只使用accessoryType to none或checkmark(视情况而定),它就可以工作

以下是我刚刚初始化某个变量时的
viewDidLoad
代码:

-(void)viewDidLoad 
{
    [super viewDidLoad];

    //Let me select more than one row
    self.tableView.allowsMultipleSelection = YES;

//Used to store all the cells that were selected
      self.selectedIndexPaths = [[NSMutableArray alloc] init];

}
我得到了1个部分和20行(稍后它将成为一个数组,但这没有问题)

这是我的
didSelectRowAtIndexPath
函数:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.cellCheckboxImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"checkbox.png"]];


    if(![self.selectedIndexPaths containsObject:indexPath])
    {
        [self.selectedIndexPaths addObject:indexPath];
    }

    NSLog(@"%ld", (long)indexPath.row);

}
这里是
didDeceloseRowatingIndexPath
函数(我觉得是这个问题)

你为什么不省略呢

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
试着

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(![self.selectedIndexPaths containsObject:indexPath])
    {
        [self.selectedIndexPaths addObject:indexPath];
    }
    else
    {
        [self.selectedIndexPaths removeObject:indexPath];
    }
    [tableView reloadData];
    NSLog(@"%ld", (long)indexPath.row);
}

您可以这样做,不需要实现
-(void)tableView:(UITableView*)tableView-didderowatindexpath:(nsindepath*)indepath

你可以这样试试

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomeCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
   if(cell == nil)
   {
       cell = [[CustomeCellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
   }
   if([selectedPaths containsObject:indexPath])
       cell.checkMarkImage.image = [UIImage imageNamed:@"check.png"];
   else
       cell.checkMarkImage.image = [UIImage imageNamed:@"uncheck.jpg"];

return cell;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{  

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //get the custom cell
    CustomeCellTableViewCell *cell = (CustomeCellTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    if(cell)
    {
        if([selectedPaths containsObject:indexPath])
        {
           [selectedPaths removeObject:indexPath];
           cell.checkMarkImage.image = [UIImage imageNamed:@"uncheck.jpg"];
        }
        else
        {
           [selectedPaths addObject:indexPath];
           cell.checkMarkImage.image = [UIImage imageNamed:@"check.png"];
         }
   }
   //deselect the cell with animation
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

这是为我工作,希望它也为你工作

我们可以用一种方法来管理它

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    tablecell *cell = (tablecell *)[tableView cellForRowAtIndexPath:indexPath];
    UIImage *checkImage = [UIImage imageNamed:@"check.png"];
    NSData *dataCheck = UIImagePNGRepresentation(checkImage);
    NSData *dataCheckCell = UIImagePNGRepresentation(cell.img.image);
    if ([dataCheckCell isEqualToData:dataCheck]) {
        cell.img.image = [UIImage imageNamed:@"uncheck.png"];
    }
    else
    {
        cell.img.image = [UIImage imageNamed:@"check.png"];
    }
}

让我知道,如果你在上面的代码块。将帮助您解决此问题。

我以前遇到过这种情况,必须使用indexPathForVisibleRows以可怕的方式对其进行破解以使其正常工作。有兴趣看看是否有合法的解决方案。如果这对你有帮助,请投票支持我:P
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CustomeCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
   if(cell == nil)
   {
       cell = [[CustomeCellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
   }
   if([selectedPaths containsObject:indexPath])
       cell.checkMarkImage.image = [UIImage imageNamed:@"check.png"];
   else
       cell.checkMarkImage.image = [UIImage imageNamed:@"uncheck.jpg"];

return cell;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{  

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //get the custom cell
    CustomeCellTableViewCell *cell = (CustomeCellTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    if(cell)
    {
        if([selectedPaths containsObject:indexPath])
        {
           [selectedPaths removeObject:indexPath];
           cell.checkMarkImage.image = [UIImage imageNamed:@"uncheck.jpg"];
        }
        else
        {
           [selectedPaths addObject:indexPath];
           cell.checkMarkImage.image = [UIImage imageNamed:@"check.png"];
         }
   }
   //deselect the cell with animation
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    tablecell *cell = (tablecell *)[tableView cellForRowAtIndexPath:indexPath];
    UIImage *checkImage = [UIImage imageNamed:@"check.png"];
    NSData *dataCheck = UIImagePNGRepresentation(checkImage);
    NSData *dataCheckCell = UIImagePNGRepresentation(cell.img.image);
    if ([dataCheckCell isEqualToData:dataCheck]) {
        cell.img.image = [UIImage imageNamed:@"uncheck.png"];
    }
    else
    {
        cell.img.image = [UIImage imageNamed:@"check.png"];
    }
}