selectUITableViewCell AccessoryView,与select行分开
我有一个UITableview
,我正在显示一些任务,每一行都有一个checkbox来标记任务是否完成。
我打算在用户点击checkbox时切换复选标记,并在用户点击该行时切换到详细视图。 后者很容易,只是通过使用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
然而,我想分开select区域,当select了accessoryview
视图时仅切换checkbox,并且仅在select其余单元格时进入详细视图。 如果我在accessoryview
添加一个UIbutton
,用户只需要点击checkbox就可以select行和UIButton
,对吧?
另外,如果用户只是沿着accessoryview
视图拖动桌面视图,该怎么办? 这不会触发TouchUp上的UIButton
行动?
任何人有任何想法如何做到这一点? 谢谢你的时间!
在这个委托方法内部pipe理配件水龙头如何:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
编辑:
您可以对自定义的accessoryView执行类似这样的操作,以响应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]; }