触摸时更改细节披露指示图像

时间:2013-04-02 20:23:04

标签: iphone ios objective-c uitableview

是否可以在触摸时更改UITableViewCellAccessoryDetailDisclosureButton图片?

我想让tableView中的行有一个空圆代替详细信息披露按钮。当我点击这个空圆圈按钮时,我想用另一个包含复选标记的图像更改空圆圈的图像。然后在延迟大约半秒之后,我想用-accessoryButtonTappedForRowWithIndexPath执行操作。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

首先,您必须将您的单元格的accessoryView设置为自定义UIView,可能是UIButton ...

UIImage *uncheckedImage = [UIImage imageNamed:@"Unchecked.png"];
UIImage *checkedImage = [UIImage imageNamed:@"Checked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(44.0, 44.0, image.size.width, image.size.height);
[button addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:uncheckedImage forState:UIControlStateNormal];
[button setImage:checkedImage forState:UIControlStateSelected];
cell.accessoryView = button;

在tapButton:方法中,您要对图像应用必要的更改并执行accessoryButtonTappedAtIndexPath。我只是避免延迟,或者你可以使用调度计时器......

- (void)tapButton:(UIButton *)button {
  [button setSelected:!button.selected];
  UITableViewCell *cell = [button superview];
  NSIndexPath *indexPath = [tableView indexPathForCell:cell];
  [self tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];

}

答案 1 :(得分:1)

根据您的评论,您已经创建了自己的按钮并将其设置为单元格的accessoryView

创建按钮时,将其所选状态的图像设置为复选标记图像:

[button setImage:checkmarkImage forState:UIControlStateSelected];

点击按钮时,将其状态设置为选中状态,以便显示复选标记:

- (IBAction)buttonWasTapped:(id)sender event:(UIEvent *)event {
    UIButton *button = sender;
    button.selected = YES;

然后,禁用用户交互,以便用户在延迟期间无法执行任何其他操作:

    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];

获取包含触摸按钮的单元格的索引路径:

    UITouch *touch = [[event touchesForView:button] anyObject];
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:
        [touch locationInView:tableView]];

最后,安排一个块在延迟后运行。在该块中,重新启用用户交互并向您自己发送包含索引路径的消息:

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC),
        dispatch_get_main_queue(),
    ^{
        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
        [self accessoryButtonTappedForRowAtIndexPath:indexPath];
    });
}