选择另一个单元格后如何设置自定义单元格的图像? [英] How to set image of custom cell after selecting another cell?
问题描述
我正在开发一个应用程序,其中我根据某些条件为单元格设置了一些图像.
I am developing an application in which i have set some images for cells based on some criteria.
选择自定义单元格后,我正在更改它的图像,但是当我尝试选择另一个自定义单元格时.
After selecting a custom cell i am changing it's image but when i try to select another custom cell.
前一个自定义单元格的图像保持选中状态,也是新选择的一个.
the image for the previous custom cell remain selected and also the newly selected one.
我想要的是之前选择的自定义单元格的图像应该设置为默认图像我一开始就给了.
What i want is that the image of previously selected custom cell should be set to default image which i have given initially.
请帮助我解决这个问题.
Kindly help m with this.
谢谢,
推荐答案
将您的条件放在 cellForRowAtIndexPath:
方法中,并且每次更新您选择的单元格时可能都在 didSelectRowAtIndexPath:
制作一个 [self.tableView reloadData]
使 tableView 重新加载数据并再次调用它的委托方法.希望有所帮助.
Put your criteria in the cellForRowAtIndexPath:
method and everytime you update your selected cell probably in didSelectRowAtIndexPath:
make a [self.tableView reloadData]
that makes the tableView to reload the data and call it's delegate methods again.
Hope that helps.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(requirementMet){ // if your criteria where met
cell.imageView.image = [UIImage imageNamed:@"specialImage.png"];
} else{
cell.imageView.image = [UIImage imageNamed:@"defaultCellImage.png"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//do your select things here
[self.tableView reloadData];
}
这篇关于选择另一个单元格后如何设置自定义单元格的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!