我有一个 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/