如何切换UITableView Cell选择状态 [英] How to toggle UITableView Cell selected status
问题描述
我有一个带有自定义Cell的UITableView,该单元格包含一个UIImageView和一个UILabel。现在当我第一次加载我的表时,它会在每个单元格和不同标签上加载相同的图像,它从LabelArray中获取。
I have a UITableView with a custom Cell, the cell contains a UIImageView and a UILabel. Now When I load my table first time, It loads with a same image on each cell and different labels, which it takes from the LabelArray.
现在我正在谈论的图像about是一个radioButton,所以当用户点击单元格时,图像会发生变化。如果用户再次点击它会更改为默认状态。
Now the image I am talking about is a radioButton, So when the user clicks the cell, the image changes. If user clicks again it changes to default state.
为此,我使用了这个函数,并且我在customcell类中声明了一个名为selectionStatus的bool变量。
For this to happen, I have used this function and also I have declared a bool variable in my customcell class called 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属性)并且我能够获得该效果。但是现在当我关闭View并再次打开它时,函数
This works fine, (but I want to know whether it is a proper way, or can we check the cell.selected property) and I am able to get that effect. But now when I close the View and open it again the function
根据以下评论编辑@Anil
Edit based on below comments with @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;
问候
Ranjit
Regards Ranjit
推荐答案
您必须在didSelectRowAtIndexPath中保存所选行的索引路径并检查cellForRowAtIndexPath中的索引路径:设置相应的图像
you have to save the index path of the selected row in didSelectRowAtIndexPath and check the index path in cellForRowAtIndexPath: set the corresponding image
你想要多重选择吗?试试这个...
Do you want multiple selection? try this...
- (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中检查如下
Edit
In cellForIndexPath check like this
// 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
}
确实我们会看到
这篇关于如何切换UITableView Cell选择状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!