2016-08-05 68 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

謝謝,但我試圖得到用戶輸入時,按鈕被點擊。這裏不會這樣做,因爲在輸入改變後用戶可能不會選擇該行。 –

相關問題