我有一个带有自定义Cell的UITableView,该单元格包含一个UI ImageView和一个UILabel.现在当我第一次加载我的表时,它会在每个单元格和不同的标签上加载相同的图像,它从LabelArray中获取. 现在我
现在我说的图像是radioButton,所以当用户点击单元格时,图像会发生变化.如果用户再次单击,则会更改为默认状态.
为此,我使用了这个函数,并且我在customcell类中声明了一个名为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属性),我能够得到这种效果.但现在当我关闭View并再次打开它的功能时
使用@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;
问候
兰吉特
你想要多重选择吗?试试这个…
- (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 }
确实,我们会看到