更改细节公开按钮的图像(Xcode 4.2)

Old*_*Dog 5 xcode uitableview ios5

我已经使用以下建议的例程来更改表视图单元格中的详细信息公开按钮图像(在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;
}
Run Code Online (Sandbox Code Playgroud)

但是,tableView accessoryButtonTappedForRowWithIndexPath现在不再单击按钮调用该事件.

谁有任何想法为什么?

Rhu*_*arb 6

我已经给弗莱的答案+1了,但只是为了从其他网站提取代码来使SO更加完整:答案是你必须自己设备上配置.呼叫将不会自动进行,因为它会为内置的细节披露,所以你必须自己使用按钮的目标.

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

[myAccessoryButton addTarget:self action:@selector(accessoryButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

然后在以后添加:

- (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];
    }
} 
Run Code Online (Sandbox Code Playgroud)

(从此链接逐字复制)

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

说明:accessoryButtonTapped是我们自定义按钮的自定义目标方法.当按下按钮时("TouchUpInside"),我们在发生按压的位置找到单元格的indexPath,并调用表视图通常会调用的方法.