iphone - 选择 UITableViewCell AccessoryView,与选择行分开

标签 iphone objective-c ios uitableview

我有一个 UITableview,我正在显示一些任务,每一行都有一个复选框来标记任务是否完成。

我希望在用户点击复选框时切换复选标记,并在用户点击该行时切换到详细 View 。后者很简单,只需使用

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

但是,我想分开选择区域,只在选择 accessoryview 时切换复选框,只有在选择单元格的其余部分时才进入详细 View 。如果我在 accessoryview 中添加一个 UIbutton,当用户只想点击复选框时,他们会选择该行和 UIButton,对吧?

此外,如果用户只是通过拖动 accessoryview 来滚动浏览 tableview 怎么办?这不会触发 TouchUp 上 UIButton 的操作吗?

有人知道如何做到这一点吗?感谢您的宝贵时间!

最佳答案

如何在这个委托(delegate)方法中管理附件水龙头:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

编辑:

您可以为响应 accessoryButtonTappedForRowWithIndexPath: 方法的自定义 accessoryView 执行类似的操作。

cellForRowAtIndexPath: 方法中 -

BOOL checked = [[item objectForKey:@"checked"] boolValue];
UIImage *image = (checked) ? [UIImage   imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];

[button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

- (void)checkButtonTapped:(id)sender event:(id)event
{
   NSSet *touches = [event allTouches];
   UITouch *touch = [touches anyObject];
   CGPoint currentTouchPosition = [touch locationInView:self.tableView];
   NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
   if (indexPath != nil)
  {
     [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
  }
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
  NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row];
  BOOL checked = [[item objectForKey:@"checked"] boolValue];
  [item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];

  UITableViewCell *cell = [item objectForKey:@"cell"];
  UIButton *button = (UIButton *)cell.accessoryView;

  UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
  [button setBackgroundImage:newImage forState:UIControlStateNormal];
}

关于iphone - 选择 UITableViewCell AccessoryView,与选择行分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12189191/

相关文章:

objective-c - UIWebView:使框架适合 View 大小,允许在 UISplitViewController 上的框架内滚动

iphone - 如何在 iOS 中列出声音文件?

iphone - 制作多 View 应用程序的有效方法?

ios - 当前位置的蓝点未使用适用于 iOS 的谷歌地图显示

iphone - 如何将委托(delegate)设置为类方法

objective-c - 从 NSString 获取长值

ios - Tapkey token 交换成功,但尝试登录时收到 400 Bad Request

javascript - 如何从React-Native向Native发送参数

iphone - 比较两个 NSArray 的最佳实践 & 用两个都没有出现的对象填充第三个?

ios - 如何获取/设置iOS流音量?