【发布时间】:2025-12-06 00:50:01
【问题描述】:
我有一个带有自定义单元格的 UITableView,该单元格包含一个 UIImageView 和一个 UILabel。现在,当我第一次加载表格时,它会在每个单元格上加载相同的图像和不同的标签,这些标签来自 LabelArray。
现在我说的图像是一个单选按钮,所以当用户点击单元格时,图像会发生变化。如果用户再次单击它会更改为默认状态。
为此,我使用了这个函数,并且在我的 customcell 类中声明了一个 bool 变量,称为 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 属性)并且我能够获得这种效果。但是现在当我关闭视图并再次打开它时,功能
使用@Anil 根据下面的 cmets 进行编辑
- (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;
问候 兰吉特
【问题讨论】:
-
我建议至少使用 [tableView deselectRowAtIndexPath:indexPath animated:YES];取消选择您的单元格。
-
和 [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];选择您的单元格。为您提供用于选择和取消选择单元格的默认 IOS 动画。
-
嗨@shoughton123,谢谢你的回复,但是当表格再次加载时呢
-
只需将选择的索引保存为变量,当重新加载表时,只需使用这些方法重新选择相同的索引。
标签: ios ios5 uitableview ios6