2016-08-05 66 views
1

我有一个自定义单元格,在它UITextViewoutletUITextView在customCell文件。获取UITextView的数据,自定义单元格中迅速

现在在我的viewControllertableView我创建并添加自定义单元格到我的表。 在此之后,我无法访问UITextView并在customCell文件中获取其数据作为其outlet。我无法将插座移动到我的viewController,因为我需要它。 我该怎么做(访问UITextView在我的viewController

回答

4

您可以设置单元格的textView那代表你viewController,首先设置其委托里面你cellForRowAtIndexPath

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomTableCell 
    cell.textView.delegate = self 
    cell.textView.tag = indexPath.row //In case you have multiple textView 
    return cell 
} 

现在,你可以得到它的UITextViewDelegate方法这里面的TextView 。

func textViewDidBeginEditing(textView: UITextView) { 
    print(textView.text) 
} 
0

您需要在您的viewController中声明一个局部变量来获取UITextView的数据。然后实现tableView小号delegate方法

//this is your local variable declared in viewCOntroller 
var myTextViewText: String? 

//And this is your delegate method 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! YourCell 
    myTextViewText = cell.textView.text //here you will get the text of your cell's textView 
} 
+0

谢谢,但我试图得到用户输入时,按钮被点击。这里不会这样做,因为在输入改变后用户可能不会选择该行。 –

相关问题