Iphone 触摸时更改详细信息披露指示灯图像
触摸时是否可以更改Iphone 触摸时更改详细信息披露指示灯图像,iphone,ios,objective-c,uitableview,Iphone,Ios,Objective C,Uitableview,触摸时是否可以更改UITableViewCellAccessoryDetailDisclosure按钮的图像 我想让我在tableView中的行有一个空圆圈来代替详细信息披露按钮。当我点击这个空圆圈按钮时,我想用另一个包含复选标记的图像来更改空圆圈的图像。然后,在大约半秒钟的延迟后,我想使用-accessorybuttonattappedforrowwithindexpath执行一个操作 我该怎么做?根据您的评论,您已经在创建自己的按钮,并将其设置为单元格的访问视图 创建按钮时,将其选定状态的图
UITableViewCellAccessoryDetailDisclosure按钮的图像
我想让我在tableView中的行有一个空圆圈来代替详细信息披露按钮。当我点击这个空圆圈按钮时,我想用另一个包含复选标记的图像来更改空圆圈的图像。然后,在大约半秒钟的延迟后,我想使用-accessorybuttonattappedforrowwithindexpath
执行一个操作
我该怎么做?根据您的评论,您已经在创建自己的按钮,并将其设置为单元格的访问视图
创建按钮时,将其选定状态的图像设置为选中标记图像:
[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];
});
}
首先,您必须将单元格的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:方法中,您希望对图像应用必要的更改,并执行AccessoryButtonTappedIndeXPath。我会尽量避免延误,或者你可以使用调度计时器
- (void)tapButton:(UIButton *)button {
[button setSelected:!button.selected];
UITableViewCell *cell = [button superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
[self tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
@加布里埃佩特罗内拉:我已经能够自定义按钮,使其具有空圆圈。我遵循这一点,如果用户在“大约半秒的延迟”期间点击其他内容,会发生什么?@robmayoff,真的什么都没有。我认为延迟只是为了让用户有时间识别图像中的变化。好的解决方案。我会避免触摸逻辑,因为你知道按钮的超级视图就是电池。然后您可以调用[tableView indexPathForCell:cell]来获取索引路径。@BenM您不应该依赖按钮作为单元格的直接子视图。这是一个实现细节,不是公共API的一部分。有很多方法可以从按钮进入只使用公共API的单元格。使用indexPathForRowAtPoint:
是一种简单的方法。