选择 UITableViewCell AccessoryView,与选择行分开

2023-12-21

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

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

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

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

另外,如果用户只是通过拖动来滚动表格视图会怎样?accessoryview?这不会触发对UIButton在 TouchUp 上?

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


如何在该委托方法中管理附件点击:

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

EDIT:

您可以对响应的自定义accessoryView执行类似的操作accessoryButtonTappedForRowWithIndexPath: method.

In 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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

选择 UITableViewCell AccessoryView,与选择行分开 的相关文章

随机推荐