2013-04-02 114 views
2

是否可以更改触摸屏上的UITableViewCellAccessoryDetailDisclosureButton图像?更改触摸屏上的详细信息披露指示图像

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

我该怎么做?

+0

@GabrielePetronella我已经能够自定义按钮,使它有空圈。我跟着这个[链接](http://iphonedevsdk.com/forum/iphone-sdk-development/71147-change-disclosure-button.html) –

+0

如果用户在其他时间点击别的东西,会发生什么?“延迟大约一半一秒”? –

+0

@robmayoff什么都没有。我想到延迟只是为了让用户有一段时间来识别图像中的变化。 –

回答

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

根据您的评论,您已经创建了自己的按钮并将其设置为单元的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]; 
    }); 
} 
+0

好的解决方案。我会避免触摸逻辑,因为你知道按钮的超级视图是单元格。然后你可以调用[tableView indexPathForCell:cell]来获取索引路径。 –

+0

@BenM你不应该依赖该按钮作为单元格的直接子视图。这是一个不属于公共API的实现细节。有许多方法可以从按钮访问仅使用公共API的单元格。使用'indexPathForRowAtPoint:'是一个简单而简单的方法。 –

相关问题