我创建了一个自定义单元格来显示一个文本和 2 个图像,当用户选择该单元格时,图像应该会改变。我可以访问单元格的属性,但不能更改它们:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
CustomCell *cell = (CustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.check.image = setImage:[UIImage imageNamed:@"1355327732_checkbox-checked"];
[cell.check setImage:[UIImage imageNamed:@"1355327732_checkbox-checked"]]; }
cell.check is a UIImageView
我错过了什么吗?
最佳答案
如果您使用的是自定义单元格,那么您可以像这样覆盖函数 setSelected:animated: ...
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
self.check.image = [UIImage imageNamed:@"1355327732_checkbox-checked"];
} else {
self.check.image = nil;
}
}
那么您不必在 tableView 代码中做任何事情来改变它。它会正常工作。
一个更好的替代方法是在 self.check 中保持图像相同。然后你可以相应地将 hidden 设置为 YES 或 NO。这也会提高性能。
为了让您从表中获得多个选择,然后在 TableViewController 中放置...
self.tableView.allowsMultipleSelection = YES;
这将设置它以便您可以选择多行。一次点击选择,另一次点击取消选择。
要获取选定的行,您可以运行...
NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];
关于ios - 无法更改所选自定义单元格中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13844724/