我已经使用以下建议的例程来更改表视图单元格中的详细信息公开按钮图像(在tableView cellForRowAtIndexPath中)
if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0,24,24)]; [myAccessoryButton setBackgroundColor:[UIColor clearColor]]; [myAccessoryButton setImage:[UIImage imageNamed:@"ball"] forState:UIControlStateNormal]; [cell setAccessoryView:myAccessoryButton]; cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; }
但是,单击按钮时,现在不再调用事件tableView accessoryButtonTappedForRowWithIndexPath.
谁有任何想法为什么?
解决方法
我已经给了弗莱的答案1,但只是从其他网站提取代码以使其更加完整:答案是你必须自己设备上配置.呼叫将不会自动生成,因为它将建立 – 详细信息披露,所以你必须自己使用按钮的目标.
从链接代码转换到上面的示例,在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,并调用表视图通常会调用的方法.