ios – 在Swift 2中实现accessoryButtonTappedForRowWithIndexPath:

我正在尝试在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的代码,我可以这样做.我错过了什么?

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)
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...