我有一个带有自定义单元格的 UITableView,该单元格包含一个 UIImageView 和一个 UILabel。现在,当我第一次加载表格时,它会在每个单元格上加载相同的图像和不同的标签,这些标签是从 LabelArray 中获取的。
现在我所说的图像是一个单选按钮,因此当用户单击该单元格时,图像会发生变化。如果用户再次单击,它将更改为默认状态。
为了实现这一点,我使用了这个函数,并且还在我的自定义单元类中声明了一个名为 SelectionStatus 的 bool 变量。
- (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 属性)并且我能够获得该效果。但现在当我关闭视图并再次打开它时,该功能
根据@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;
问候
兰吉特
切换 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)