我有一个包含tableview的视图控制器,这个tableview包含在storyboard中完成的动态单元格。大多数单元格包含一个文本框,并且在视图控制器中,我需要检测文本框何时被选中以及它是哪个单元格。 (我加载指向单元格的弹出窗口,并且这必须从视图控制器中调用)。如何知道tableview中的哪个单元格已被选中
单元代码
import UIKit
class AddNew_Date_Cell: UITableViewCell {
@IBOutlet var Label: UILabel!
@IBOutlet var textField: UITextField!
func loadItem(var data:NSArray) {
Label.text = data[0] as? String
}
}
ViewControllerCode
import UIKit
class AddNewDetailView: UIViewController, UITableViewDelegate, UITableViewDataSource, UIPopoverPresentationControllerDelegate, UITextFieldDelegate {
@IBOutlet var tableView: UITableView!
var items : NSMutableArray!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch(items[indexPath.row][1] as! Int)
{
...
case 2:
var cell:AddNew_Date_Cell = tableView.dequeueReusableCellWithIdentifier("AN_Date_Cell") as! AddNew_Date_Cell
cell.loadItem(items[indexPath.row] as! NSArray, view: self)
cell.textField.delegate = self
return cell
...
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
//Need to know here which cell the textfield is attached to
turn true
}
}
每次我选择我打一个断点“textFieldShouldBeginEditing”一个文本框观点,但是我不知道哪个单元的英寸
如果我选择单元格中的textfield“didSelectRowAtIndexPath”永远不会被命中。
我如何知道哪个单元格已被选中。
感谢
你最近怎么样使用这些信息? (选择哪个单元格)?不同的方法,无论你需要获取单元格的NSIndexPath还是indexPath.row来操作你的项目数组 – Dustin
我将把数据拉出单元格(当前文本值)并加载指向它的弹出窗口 –
“弹出窗口指向”你的意思是加载一个“视觉上”指向单元格的popover? – Dustin