xcode - 更改详细信息披露按钮的图像(Xcode 4.2)

标签 xcode uitableview ios5

我已使用以下建议例程更改表格 View 单元格中的详细信息披露按钮图像(在 tableView cellForRowAtIndexPath 中)

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
    [myAccessoryButton setBackgroundColor:[UIColor clearColor]];
    [myAccessoryButton setImage:[UIImage imageNamed:@"ball"] forState:UIControlStateNormal];
    [cell setAccessoryView:myAccessoryButton];
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}

然而,事件tableView accessoryButtonTappedForRowWithIndexPath现在不再在单击按钮时调用。

有没有人知道为什么?

最佳答案

我已经对 Fry 的回答进行了 +1,但只是为了从其他站点提取代码以使其更加完整:答案是您必须使用附件按钮Tapped .... 调用不会像它那样自动进行内置详细信息披露,因此您必须自己使用按钮的目标来完成。

将链接代码转换为上面的示例,在 setImage 之后添加以下行:

[myAccessoryButton addTarget:self action:@selector(accessoryButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];

然后稍后添加:
- (void)accessoryButtonTapped:(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];
    }
} 

(从 this link 逐字复制)

请注意,这假定按钮是在 UITableViewController 中创建的(在我的情况下,我在自定义单元格中创建一个,因此引用略有不同)

说明:accessoryButtonTapped 是我们自定义按钮的自定义目标方法。当按钮被按下时(“TouchUpInside”),然后我们找到按下发生点的单元格的 indexPath,并调用表格 View 通常会调用的方法。

关于xcode - 更改详细信息披露按钮的图像(Xcode 4.2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7797843/

相关文章:

iphone - 核心图创建动态Y轴并非常清晰地显示图形

ios - View 左上角的 GMSMarker 图标 (iOS)

swift - 在以编程方式实例化的 UIViewController 中测试 UITableView

iphone - NSJSONSerialization 出错 - JSON 写入中的类型无效(菜单)

objective-c - 创建自定义 UITableViewCell

ios - UISearchController 位置错误,因为带有 tableview 插入

iphone - 如何区分没有 Twitter 访问权限的应用程序和没有帐户设置的 Twitter?

iphone - Xcode 本地化更新

ios - 如何让 iPhone X home bar 横屏双击?

Xcode 5 - 无法构建模块