我有一个应用程序,当用户选择一个新单元格时,我需要在其中取消选择所选单元格。我可以选择和取消选择 tableview 行,但问题是我一次只需要选择一行。我现在就是这样做的,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell_check.png"]];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell.png"]];
}
但在这里我可以选择和取消选择同一行。但我的意图是在选择新行时,如果已经选择了任何其他单元格,则需要取消选择。任何人都可以帮助我吗?
最佳答案
一个天真的解决方案是
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
[tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell_check.png"]];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:@"white_cell.png"]];
}
基本上,当您选择一个新单元格时,您将遍历所有单元格并取消选择它们。
关于iphone - 在表格 View 中选择新行时如何取消选择选定的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14534539/