在Swift 2中实现accessoryButtonTappedForRowWithIndexPath:

7
我正在尝试在Swift 2中实现accessoryButtonTappedForRowWithIndexPath:,并且使用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

在我的代码中,detailDisclosure chevron被绘制出来了,但我分配给它的目标动作方法没有被调用。
然后,我需要为按钮创建按下时的处理程序:
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个回答

17

我不确定为什么你在代码中添加披露按钮指示器。

你要做的只是两个简单的步骤 -

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

cell.accessoryType = .DetailDisclosureButton

步骤 2 : 通过创建 accessoryButtonTappedForRowWithIndexPath 函数,在按钮被点击时采取行动:

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

那就是我的问题。DisclosureIndicator只是装饰性的,除了被绘制出来之外没有任何作用。DetailDisclosureButton实际上可以与操作相关联。感谢您帮我解决了这个问题。 - Adrian
1
很高兴能帮到你!祝你好运 :)! - Abhinav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接