在Swift 2中实现accessoryButtonTappedForRowWithIndexPath:

Adr*_*ian 7 uitableview ios swift swift2

我正试图accessoryButtonTappedForRowWithIndexPath:在Swift 2中实现UITableViewController.

正如我在下面解释的那样,我认为在创作时我会遗漏一些东西disclosureIndicator,但我不知道是什么.它来自代码,但我的目标操作不会被调用.啊!

要以编程方式执行此操作,我的理解是我需要在返回cellForRowAtIndexPath之前添加detailDisclosure指示符cell.我这样做如下:

// 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
Run Code Online (Sandbox Code Playgroud)

在我的代码中,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
}
Run Code Online (Sandbox Code Playgroud)

最后accessoryButtonTappedForRowWithIndexPath包含执行segue的代码,我可以这样做.我错过了什么?

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

Abh*_*nav 17

不确定为什么要在代码中添加类似的披露按钮指示器.

您正在寻找的只是两个步骤 -

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

cell.accessoryType = .DetailDisclosureButton
Run Code Online (Sandbox Code Playgroud)

第2步:通过创建accessoryButtonTappedForRowWithIndexPath功能点击按钮时执行操作:

override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
    doSomethingWithItem(indexPath.row)
}
Run Code Online (Sandbox Code Playgroud)