2015-10-18 57 views
6

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

正如我在下面解释的,我想我在创建disclosureIndicator时错过了一些东西,但我不知道是什么。它从代码中获取,但我的目标操作不会被调用。啊!

以编程方式做到这一点,我的理解是,我需要回到我的cell之前添加detailDisclosure指示器cellForRowAtIndexPath。我做了如下:

// 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

回答

12

不知道为什么你在代码中增加披露按钮指示灯这样。

什么你正在寻找简单地说就是两个步骤 -

第1步:细胞添加正确accessoryType

cell.accessoryType = .DetailDisclosureButton 

第2步:当按钮被窃听采取行动创建accessoryButtonTappedForRowWithIndexPath功能:

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

这是我的问题。 “DisclosureIndicator”只是眼睛的糖果,除了被绘制之外不会做任何事情。 'DetailDisclosureButton'实际上可以有与之相关的操作。谢谢你让我摆平。 – Adrian

+1

很高兴帮助!欢呼:)! – Abhinav