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

3
我有一个带有自定义单元格的UITableView,该单元格包含UIImageView和UILabel。现在,当我第一次加载我的表格时,它会在每个单元格上加载相同的图像和不同的标签,这些标签来自LabelArray。
现在,我说的图像是一个radioButton,因此,当用户点击单元格时,图像会改变。如果用户再次点击,则将其更改为默认状态。
为了实现这一点,我使用了这个函数,并在我的customcell类中声明了一个名为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;
        }
    }
}

这样做没问题,但我想知道是否这是一种正确的方式,或者我们可以检查cell.selected属性。我能够得到所需的效果。但现在,当我关闭视图并再次打开它时,该函数...
- (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;

问候 Ranjit


我建议至少使用[tableView deselectRowAtIndexPath:indexPath animated:YES];来取消选择您的单元格。 - shoughton123
使用 [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; 来选择你的单元格。这会为你提供默认的 iOS 动画效果,用于选中和取消选中单元格。 - shoughton123
嗨@shoughton123,感谢您的回复,但是如果表格再次加载呢? - Ranjit
只需将所选索引保存为变量,当表格重新加载时,只需使用这些方法重新选择相同的索引即可。 - shoughton123
4个回答

6
另一种切换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; 
}

在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
    }; 
}

最后,在viewDidLoad中设置clearsSelectionOnViewWillAppear属性...
- (void)viewDidLoad {
    [super viewDidLoad];
    self.clearsSelectionOnViewWillAppear = NO;
}

3

在didSelectRowAtIndexPath中,您需要保存所选行的索引路径,并在cellForRowAtIndexPath:中检查索引路径,设置相应的图像。

您是否想要多重选择?尝试这个...

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

if(indexPath.row == 0)
{
    if(cell.selectionStatus == YES)
    {     
      [self.checkedIndexPaths addObject:indexPath];
        //Do your stuff
       cell.selectionStatus = NO;
    }
    else
    {
        [self.checkedIndexPaths removeObject:indexPath];
        //Do your stuff
        cell.selectionStatus = YES;
    }
}
}  

编辑
在 cellForIndexPath 中进行如下检查:

 // Set the default image for the cell. imageXYZ   
for (NSIndexPath *path in self.checkedIndexPath) 
{
    if ([path  isEqual:indexPath])
    {
        //set the changed image for the cell. imageABC
    }
    // no need of else part
}

按照我们要求做,我们会看到准确的结果。


好的,没问题。我会把indexpath保存在一个数组中,但是用户可能会点击100次,每次点击都会被添加到数组中。 - Ranjit
嗨@Anil,感谢您的编辑,如何在CellForRowAtIndexPath中进行检查? - Ranjit
尝试在cellForRow枚举时打印所有checkedIndexPath.row,以确保它们各不相同。 - Anil Varghese
嗨@Anil,请检查我上面的代码。同时,我通过NSLog检查了IndexPath: "<NSIndexPath 0xa570390> 2索引 [0, 0]", "<NSIndexPath 0xa5a3670> 2索引 [0, 1]", "<NSIndexPath 0xa5a7b30> 2索引 [0, 2]" - Ranjit
伙计,你的方法不正确。我来纠正代码,请等一下。 - Anil Varghese
显示剩余4条评论

0
首先,为了切换目的,在tableview cell类中声明一个布尔值selectionStatus,如下所示:
@property(nonatomic)BOOL selectionStatus;
声明一个NSMutableArray来存储所选索引路径(用于多选目的)
self.checkedIndexPaths = [[NSMutableArray alloc] init];
在didSelectRowAtIndexPath中,
a.如果布尔值selectionStatus为No,则将indexPath插入NSMutableArray checkedIndexPaths,并设置cell.selectionStatus = YES
b.如果布尔值selectionStatus为Yes,则从NSMutableArray checkedIndexPaths中删除indexPath,并设置cell.selectionStatus = NO
c.每次重新加载tableView
以下是代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    tableCell = [self.tableView cellForRowAtIndexPath:indexPath];


        if(cell.selectionStatus == YES)
        {
            [self.checkedIndexPaths removeObject: indexPath];
            cell.selectionStatus = NO;

        }
        else
        {

            [self.checkedIndexPaths addObject: indexPath];
            cell.selectionStatus = YES;


        }

    [self.tablee reloadData];
}
  • 在cellForRowAtIndexPath方法中

    a. 首先将单元格图像设置为未选中,即此处的xyz.png

    [tableCell.selectionImage setImage:[UIImage imageNamed:@"xyz.png"]];

    b. 为NSMutable数组self.checkedIndexPaths中的所有索引路径设置已选中的图像

    for (NSIndexPath *path in self.checkedIndexPaths)
    {
        if ([path  isEqual:indexPath])
        {
            [tableCell.selectionImage setImage:[UIImage imageNamed:@"abc.png"]];
        }
    
    }
    

  • 0
    在您的模型类中使用布尔值,并在didSelectRow方法中更新它,然后重新加载表格。在cellForRowAtIndexPath中检查它是否为真。
     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
     After making your cell check like that 
    
        ModelClass *yourModel=[self.yourArray objectAtIndex:indexPath.row];
       if(yourModel.selection)
         [cell.customImage setImage:[UIImage imageName:@"selected"]; 
      else
         [cell.customImage setImage:[UIImage imageName:@"un_selected"];
    

    }

    请查看此链接 https://github.com/adnanAhmad786/Quiz-View--TableView


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接