我在做一個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)
}
}
如果我這樣做,我有這樣的錯誤:不能分配型ParagraphCell的價值鍵入UITextViewDelegate? – fandro
您需要將UITextViewDelegate添加到ParagraphCell協議聲明 – tommybananas
'class ParagraphCell:UITableViewCell,UITextViewDelegate {' – tommybananas