在所选索引处更改tableview单元格中的Imageview图像

时间:2018-04-13 06:58:19

标签: ios objective-c

如果我为之前的cell选择了另一个cell图片保持相同,请帮助我。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    VoteDeatailTableViewCell *cell = [_voteTable dequeueReusableCellWithIdentifier:@"VoteDeatailTableViewCell"];
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.imgRadio.image  = [UIImage imageNamed:@"radio_uncheck"];
    return cell;

}

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

    VoteDeatailTableViewCell *cell = [_voteTable cellForRowAtIndexPath:indexPath];
    cell.imgRadio.image  = [UIImage imageNamed:@"radio_check"];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

1 个答案:

答案 0 :(得分:1)

如果您在imageView中更改了单元格didSelectRowAtIndexPath,那么当您滚动tableView它不匹配时。

所以我的建议是在indexPathNSMutableArray中添加选定的cellForRowAtIndexPath检查该数组是否包含选定的indexPath,如果它包含设置radio_check的图像,否则设置radio_uncheck图像,如下所示。全局定义NSMutableArray

NSMutableArray *arrSelectedImages = [[NSMutableArray alloc] init];


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    VoteDeatailTableViewCell *cell = [_voteTable dequeueReusableCellWithIdentifier:@"VoteDeatailTableViewCell"];
    cell.contentView.backgroundColor = [UIColor clearColor];

    if ([arrSelectedImages containsObject:indexPath]) {
        cell.imgRadio.image  = [UIImage imageNamed:@"radio_check"];
    }
    else {
        cell.imgRadio.image  = [UIImage imageNamed:@"radio_uncheck"];
    }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [arrSelectedImages removeAllObjects];
    [arrSelectedImages addObject:indexPath];
    [self.tblVW reloadData];
}