我正在尝试在accessoryButtonTappedForRowWithIndexPath:的Swift 2中实现UITableViewController

正如我在下面解释的那样,我认为我在创建disclosureIndicator时缺少了一些东西,但是我不知道是什么。它是从代码中提取的,但是没有调用我的目标 Action 。啊!

要以编程方式执行此操作,我的理解是我需要在返回cellForRowAtIndexPath之前在cell中添加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人字形,但是没有调用我为其分配的目标操作方法。

然后,我需要在按下按钮时为该按钮创建一个处理程序:
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的代码,我可以这样做。我想念什么?

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/tableView:accessoryButtonTappedForRowWithIndexPath:

最佳答案

不确定为什么要在代码中添加披露按钮指示符。

您正在寻找的只是两步过程-

步骤1:在单元格上添加正确的accessoryType:

cell.accessoryType = .DetailDisclosureButton

步骤2:通过创建accessoryButtonTappedForRowWithIndexPath函数,在点击按钮时执行操作:
override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
    doSomethingWithItem(indexPath.row)
}

10-08 02:29