swift2 实现附件按钮为具有索引路径的行点击:在Swift 2中

yftpprvb  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(183)

我尝试在Swift 2中的UITableViewController上实现accessoryButtonTappedForRowWithIndexPath:
正如我在下面解释的那样,我认为在创建disclosureIndicator时我遗漏了一些东西,但我不知道是什么。它是从代码中提取的,但我的目标操作没有被调用。
要以编程方式完成此操作,我的理解是,在返回cell之前,需要在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的代码,我可以做到。我遗漏了什么?
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/tableView:accessoryButtonTappedForRowWithIndexPath:

hwazgwia

hwazgwia1#

不确定为什么要在代码中添加类似的显示按钮指示器。
您所寻找的是简单的两步流程-

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

cell.accessoryType = .DetailDisclosureButton

**第2步:**通过创建accessoryButtonTappedForRowWithIndexPath函数,在点击按钮时执行操作:

override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
    doSomethingWithItem(indexPath.row)
}

相关问题