【问题标题】:Changing the Image for a detail disclosure button (Xcode 4.2)更改细节披露按钮的图像 (Xcode 4.2)
【发布时间】:2011-12-09 12:23:27
【问题描述】:

我已使用以下建议的例程来更改表格视图单元格中的详细信息披露按钮图像(在 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。

有人知道为什么吗?

【问题讨论】:

    标签: xcode uitableview ios5


    【解决方案1】:

    我已经 +1 了 Fry 的答案,但只是为了从其他站点提取代码以使 SO 更完整:答案是您必须使用 accessoryButtonTapped.... 调用不会自动进行,因为它将用于内置的详细信息披露,因此您必须自己使用按钮的目标。

    将链接代码转换为上面的示例,在 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,并调用表格视图通常会调用的方法。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 2023-03-27
    相关资源
    最近更新 更多