ios - 从 UITextView Swift 2 获取文本
<p><p>我正在使用 swift 2 做一个 iOS 项目,我有一个带有自定义单元格的 UITableView。我有一个带有 UITextView 和简单标签的单元格,当它被修改时我不想得到这个 UITextView 的值,但它不像 UITextFields,我不能在编辑开始方法上使用。我该怎么做?</p>
<p>这是我定义自定义单元格的代码:</p>
<pre><code>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
</code></pre>
<p>这是我的 ParagraphCell.swift</p>
<pre><code>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)
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您需要在 awakeFromNib 中添加 <code>answerField.delegate = self</code> 以接收委托(delegate)回调。</p>
<pre><code>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
}
</code></pre>
<p>您还需要将 <code>UITextViewDelegate</code> 添加到您的类协议(protocol)声明中:</p>
<pre><code>class ParagraphCell: UITableViewCell, UITextViewDelegate { }
</code></pre></p>
<p style="font-size: 20px;">关于ios - 从 UITextView Swift 2 获取文本,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/37131507/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/37131507/
</a>
</p>
页:
[1]