iphone - 在表格 View 中选择新行时如何取消选择选定的行?

标签 iphone ios ipad

我有一个应用程序,当用户选择一个新单元格时,我需要在其中取消选择所选单元格。我可以选择和取消选择 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/

相关文章:

iphone - 有没有办法在 cocos2d 中为 UIKit 复制 moveTo 方法?

iphone - 在 Facebook 上发布链接不显示链接缩略图?

iphone - 在 UIView 上滚动有图案的背景

ios - 如何在 iPad SplitViewController 中使用 SWRevealViewController

iphone - UILocalNotification 自定义声音不在 iOS7 中播放

iphone - 提交 iPhone 应用程序时本地化的利弊

ios - encodeWithCoder 内部发生崩溃 : on replacementObjectForKeyedArchiver: method

iphone - 使用 AVAudioRecorder 录制 WAV 格式文件?

ios - UIButton 图片饱和度变化

ios - 使用 AVAudioSession 设置麦克风增益时出现不稳定行为