我正在尝试在UITableViewController上的
Swift 2中实现accessoryButtonTappedForRowWithIndexPath:
正如我在下面解释的那样,我认为在创建revealIndicator时我遗漏了一些内容,但我不知道是什么.它来自代码,但我的目标操作不会被调用.啊!
为了以编程方式执行此操作,我的理解是我需要在返回单元格之前在cellForRowAtIndexPath中添加detailDisclosure指示符.我这样做如下:
- // Create disclosure indicator button in the cell
- let disclosureIndicatorButton = UIButton(type: UIButtonType.DetailDisclosure)
- disclosureIndicatorButton.addTarget(self,action: "disclosureIndicatorPressed:event:",forControlEvents: UIControlEvents.TouchUpInside)
- customCell.accessoryType = .DisclosureIndicator
在我的代码中,detailDisclosure V形图被绘制,但我分配给它的目标操作方法不会被调用.
然后我需要在按下时为按钮创建一个处理程序:
- func disclosureIndicatorPressed(sender: UIButton,event: UIControlEvents) {
- print("disclosure button pressed")
- // convert touches to CGPoint,then determine indexPath
- // if indexPath != nil,then call accessoryButtonTappedForRowWithIndexPath
- }
最后,accessoryButtonTappedForRowWithIndexPath包含执行segue的代码,我可以这样做.我错过了什么?
解决方法
不确定为什么要在代码中添加类似的披露按钮指示器.
您正在寻找的只是两个步骤 –
第1步:在单元格上添加正确的accessoryType:
- cell.accessoryType = .DetailDisclosureButton
第2步:通过创建accessoryButtonTappedForRowWithIndexPath函数来点击按钮时执行操作:
- override func tableView(tableView: UITableView,accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
- doSomethingWithItem(indexPath.row)
- }