8

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

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

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

但是,我想分隔选择区域,仅在选中复选框时切换复选框,仅在选中accessoryview单元格的其余部分时才进入详细视图。如果我在UIbutton里面添加一个accessoryview,用户会选择行并且UIButton当他们只想点击复选框时,对吗?

另外,如果用户只是通过拖动来滚动表格视图accessoryview怎么办?这不会触发UIButtonon TouchUp 上的动作吗?

有人对如何做到这一点有任何想法吗?谢谢你的时间!

4

1 回答 1

17

在这个委托方法中管理附件水龙头怎么样:

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

编辑:

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

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];
}
于 2012-08-30T03:09:40.823 回答