2016-05-10 39 views
2

我在做一個iOS項目使用swift 2,我有一個UITableView與自定義單元格。我有一個帶有UITextView和一個簡單標籤的單元格,我不想在修改時獲取此UITextView的值,但它不像UITextFields,我無法使用編輯開始方法。我怎樣才能做到這一點 ?從UITextView獲取文本Swift 2

這裏是我的代碼來定義我的自定義單元格:

let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.ParagraphCell, forIndexPath: indexPath) as! ParagraphCell 
       cell.display(block) 
       cell.selectionStyle = UITableViewCellSelectionStyle.None 
       tableView.rowHeight = UITableViewAutomaticDimension 
       tableView.estimatedRowHeight = 160.0 
       return cell 

這裏是我的ParagraphCell.swift

import UIKit 

protocol ParagraphCell { 
    func update ParagraphCell(cell: ParagraphCell, rep: String) 
} 

class ParagraphCell: UITableViewCell { 

    @IBOutlet weak var titleLabel: UILabel! 
    @IBOutlet weak var answerField: UITextView! 


    var delegate: ParagraphCell Delegate? 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
     let borderColor : UIColor = UIColor(red: 0.85, green: 0.85, blue: 0.85, alpha: 1.0) 
     answerField.layer.borderWidth = 0.5 
     answerField.layer.borderColor = borderColor.CGColor 
     answerField.layer.cornerRadius = 5.0 

    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

    func display(block: Block){ 
     titleLabel.text = block.title 
     answerField.text = "" 
    } 

//THIS METHOD IS NEVER CALLED... 
    func textViewDidChange(textView: UITextView) { //Handle the text changes here 
     print(textView.text); //the textView parameter is the textView where text was changed 
     delegate?.update ParagraphCell(self, rep: textView.text) 
    } 


} 

回答

1

您需要在awakeFromNib添加answerField.delegate = self接收委託回調。

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
    let borderColor : UIColor = UIColor(red: 0.85, green: 0.85, blue: 0.85, alpha: 1.0) 
    answerField.layer.borderWidth = 0.5 
    answerField.layer.borderColor = borderColor.CGColor 
    answerField.layer.cornerRadius = 5.0 
    answerField.delegate = self // create delegate reference 
} 

您還需要添加UITextViewDelegate到類協議聲明:

class ParagraphCell: UITableViewCell, UITextViewDelegate { } 
+0

如果我這樣做,我有這樣的錯誤:不能分配型ParagraphCell的價值鍵入UITextViewDelegate? – fandro

+0

您需要將UITextViewDelegate添加到ParagraphCell協議聲明 – tommybananas

+0

'class ParagraphCell:UITableViewCell,UITextViewDelegate {' – tommybananas

1

是的,你需要給的UITextView的委託設置爲你想要把你的textViewDidChange方法的對象。目前,你已經在ParagraphCell類中使用了它,但是將它放入你的視圖控制器可能更有意義,並且使視圖控制器成爲委託。

1
let cell = tableView.dequeueReusableCellWithIdentifier(CurrentFormTableView.CellIdentifiers.ParagraphCell, forIndexPath: indexPath) as! ParagraphCell 
       cell.display(block) 
       cell.selectionStyle = UITableViewCellSelectionStyle.None 
       cell.answerField.delegate = self 
       tableView.rowHeight = UITableViewAutomaticDimension 
       tableView.estimatedRowHeight = 160.0 
       return cell 
1

您應該使用didEndEditing委託方法,這將在編輯完成後調用。

您可以將標記設置爲textview,因爲indexpath.row表示行號以區分委託方法中的textview。

或者你可以在自定義tableviewcell類中實現委託方法。

更好的方法是在viewcontroller中實現委託方法,並在cellforrowatindexpath中將標記設置爲textview

希望這將有助於:)

+0

當您完成textview的編輯時調用'textViewDidEndEditing'。比如如果你點擊其他控件,或者你從textview切換到其他控件或者你辭去了鍵盤等 – Lion