我有一个应用程序,当用户选择一个新单元格时,我需要取消选择所选单元格.我可以选择和取消选择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"]];
}
Run Code Online (Sandbox Code Playgroud)
但在这个我可以选择和取消选择相同的行.但我的意图是在选择新行时,如果已选择任何其他单元格,则需要取消选择.有谁能够帮我 ?
一个天真的解决方案是
- (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"]];
}
Run Code Online (Sandbox Code Playgroud)
基本上,当您选择一个新单元时,您将循环遍历所有单元格以取消选择它们.
归档时间: |
|
查看次数: |
5379 次 |
最近记录: |