如何切换UITableView单元格的选定状态

Ran*_*jit 3 uitableview ios ios5 ios6

我有一个带有自定义单元格的UITableView,该单元格包含一个UIImageView和一个UILabel。现在,当我第一次加载表时,它将在每个单元格上加载相同的图像,并加载不同的标签,这是从LabelArray中获取的。

现在我要讨论的图像是单选按钮,因此,当用户单击单元格时,图像会发生变化。如果用户再次单击,它将更改为默认状态。

为此,我使用了此函数,并且在名为selectionStatus的自定义单元类中声明了一个布尔变量。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell * cell = (CustomCell* )[tableView cellForRowAtIndexPath:indexPath];

    if(indexPath.row == 0)
    {
        if(cell.selectionStatus == TRUE)
        {           
            //Do your stuff
           cell.selectionStatus = FALSE;
        }
        else
        {
            //Do your stuff
            cell.selectionStatus = TRUE;
        }
    }

    if(indexPath.row == 1)
    {
        if(cell.selectionStatus == TRUE)
        {           
            //Do your stuff
           cell.selectionStatus = FALSE;
        }
        else
        {
            //Do your stuff
            cell.selectionStatus = TRUE;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

效果很好(但是我想知道这是否是正确的方法,或者我们可以检查cell.selected属性),并且能够获得这种效果。但是现在当我关闭视图并再次打开它时

使用@Anil根据以下评论进行编辑

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

    if([self.checkedIndexpath count]  == 0)
    {
        [tableCell.selectionImage setImage:@"xyz.png"];
    }
    else
    {        
        for (int i =0; i <[self.checkedIndexPath count]; i++)
        {
            NSIndexPath *path = [self.checkedIndexPath objectAtIndex:i];
            if ([path isEqual:indexPath])
            {               
                [tableCell.selectionImage setImage:@"abc.png"]
            }
            else
            {
                 [tableCell.selectionImage setImage:@"xyz.png"]            
             }
    }
return tableCell;
Run Code Online (Sandbox Code Playgroud)

问候兰吉特

Nat*_*ter 5

切换UItableViewCells的另一个选项...

如果已经选择了单元格,则在willSelectRowAtIndexPath中为NSIndexPath返回nil,同时还要设置_selectedIndexPath实例变量。

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    if (cell.selected) {
        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        [self.tableView.delegate tableView:self.tableView didDeselectRowAtIndexPath:indexPath];
        indexPath = nil;
    }

    _selectedIndexPath = indexPath;

    return indexPath; 
}
Run Code Online (Sandbox Code Playgroud)

在didSelectRowAtIndexPath和didDeselectRowAtIndexPath中,根据属性cell.selected更新单元格。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (cell.selected) {
        // do stuff
    } else {
        // do stuff
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (cell.selected) {
        // do stuff
    } else {
        // do stuff
    }; 
}
Run Code Online (Sandbox Code Playgroud)

最后在viewDidLoad中设置clearsSelectionOnViewWillAppear属性...

- (void)viewDidLoad {
    [super viewDidLoad];
    self.clearsSelectionOnViewWillAppear = NO;
}
Run Code Online (Sandbox Code Playgroud)