【发布时间】:2015-08-07 09:49:11
【问题描述】:
我有一个包含 tableview 的视图控制器,这个 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
}
}
视图控制器代码
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”中遇到一个断点,但是我不知道它在哪个单元格中。
如果我选择单元格中的文本字段“didSelectRowAtIndexPath”永远不会被击中。
我如何知道哪个单元格被选中了。
谢谢
【问题讨论】:
-
您将如何使用这些信息? (选择了哪个单元格)?不同的方法是否需要获取单元格的 NSIndexPath 或 indexPath.row 来操作您的项目数组
-
我要从单元格中提取数据(当前文本值)并加载一个指向它的弹出框
-
“popover pointing”你的意思是加载一个“视觉上”指向单元格的popover吗?
-
是的,所以它在视觉上指向单元格,但我也想从特定的单元格类中提取数据
标签: uitableview swift tableview