accessoryButtonTappedForRowWithIndexPath:没有被调用。

我正在创build一个详细信息披露button,正在使用数组填充… ….如何在我的类中不被调用accessoryButtonTappedForRowWithIndexPath:函数。 它是一个tableviewdelegate和tableviewdatasource委托。

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{ NSLog(@"reaching accessoryButtonTappedForRowWithIndexPath:"); [self performSegueWithIdentifier:@"modaltodetails" sender:[self.eventsTable cellForRowAtIndexPath:indexPath]]; } 

NSLog不打印到控制台,这导致我相信function不被称为…这当然是当我select一个单元格。 下面的屏幕截图显示了我的细胞设置。

在这里输入图像说明

你只是点击单元格select它,或者你真的点击单元格上的附件button指示器? 你的问题并不清楚。

accessoryButtonTappedForRowWithIndexPath适用于单击单元格内的button图标,而不是在select单元格时。

该文档说,当为indexPath的行设置附件视图时,不调用tableView:accessoryButtonTappedForRowWithIndexPath:方法。 该方法仅在accessoryView属性为零时调用,并且在使用时设置accessoryType属性以显示内置附件视图。 据我所知, accessoryViewaccessoryType是相互排斥的。 当使用accessoryType ,系统会按预期的方式调用tableView:accessoryButtonTappedForRowWithIndexPath:但是你必须自己处理另一种情况。

苹果公司做这件事的方式显示在SDK的Accessory示例项目中。 在dataSource委托的cellForRowAtIndexPath方法中,他们将目标/操作设置为自定义附件button。 由于无法将indexPath传递给动作,因此会调用辅助方法来检索相应的indexPath ,并将结果传递给委托方法:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ ... UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; ... // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; ... cell.accessoryView = button; return cell; } - (void)checkButtonTapped:(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]; } } 

出于某种原因,您的设置似乎落入了accessoryView的情况下。 您是否尝试使用代码而不是使用Interface Builder来设置accessoryType?

这似乎很相关…

披露指标 :当这个元素出现时,用户知道他们可以点击行中的任何位置来查看层次结构中的下一个级别或与列表项目相关的选项。 在另一个列表的显示中select行结果时,在一行中使用披露指标。 不要使用披露指标来显示有关列表项的详细信息; 相反,为此使用详细披露button。

详细披露button :用户点击该元素可查看有关列表项的详细信息。 (请注意,您可以在表视图以外的视图中使用此元素,以显示有关某些内容的更多详细信息;有关详细信息,请参阅“详细信息披露button”)在表视图中,使用连续的详细信息button显示有关列表项。 注意,与披露指示符不同,详细披露button可以执行与行的select分离的动作。 例如,在“电话collections夹”中,轻击该行将启动对联系人的呼叫; 点击该行中的详细披露button可以显示更多关于该联系人的信息。

根据UITableViewCellAccessoryType的文档,它是预期的行为:

 typedef enum : NSInteger { UITableViewCellAccessoryNone, UITableViewCellAccessoryDisclosureIndicator, UITableViewCellAccessoryDetailDisclosureButton, UITableViewCellAccessoryCheckmark, UITableViewCellAccessoryDetailButton } UITableViewCellAccessoryType; 

常量UITableViewCellAccessoryNone单元格没有任何附件视图。 这是默认值。

UITableViewCellAccessoryDisclosureIndicator单元格有一个像人字形一样的附件控件。 这个控制表明点击单元触发一个推动动作。 该控件不跟踪触摸。

UITableViewCellAccessoryDe​​tailDisclosureButton单元格有一个信息button和一个V形图像作为内容。 这个控制表示点击单元格允许用户configuration单元格的内容。 控制轨道接触。

UITableViewCellAccessoryCheckmark单元格的右侧有一个复选标记。 这个控制不跟踪触摸。 表视图的委托可以在其tableView:didSelectRowAtIndexPath:方法中pipe理一部分行中的复选标记(可能将复选标记限制为该部分的一行)。

UITableViewCellAccessoryDe​​tailButton单元格有一个没有V形符号的信息button。 此控件指示轻击单元格会显示有关单元格内容的其他信息。 控制轨道接触。

画了点。

如果在Storyboard中更改为“DetailDisclosure”,则该方法将会触发。 (xCode 4.6 DP3)

将最佳答案转换为Swift 3:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { ... let unseletcedImage = UIImage(named: "<imagename>") let seletcedImage = UIImage(named: "<imagename>") let button = UIButton(type: .custom) // match the button's size with the image size let frame = CGRect(x: CGFloat(0.0), y: CGFloat(0.0), width: CGFloat((unseletcedImage?.size.width)!), height: CGFloat((unseletcedImage?.size.height)!)) button.frame = frame button.setBackgroundImage(unseletcedImage, for: .normal) button.setBackgroundImage(seletcedImage, for: .selected) cell?.accessoryView = button let action = #selector(checkButtonTapped(sender:event:)) (cell?.accessoryView as? UIButton)?.addTarget(self, action: action, for: .touchUpInside) .... return cell! } @objc func checkButtonTapped(sender: UIButton?, event: UIEvent) { let touches = event.allTouches let touch = touches!.first guard let touchPosition = touch?.location(in: self.tableView) else { return } if let indexPath = tableView.indexPathForRow(at: touchPosition) { tableView(self.tableView, accessoryButtonTappedForRowWith: indexPath) } } 

这是另一种使用故事板处理披露button的方法。 您应该单击表格视图单元格并转到Connections检查器。 有一个叫Triggered Segues的部分,你可以从select行拖到你想要继续的UIViewController。 segue将自动发生,您可以捕获prepareForSegue来捕获通知。
揭示button不会调用accessoryButtonTappedForRowWithIndexPath函数。 它只被请求详细的披露button。