[英]Create custom tableView cell textField or textView tag
I have multiple tableView, each tableView I have txtName and txtNote. 我有多个tableView,每个tableView我都有txtName和txtNote。 txtName is textField, txtNote is textView. txtName是textField,txtNote是textView。 I try to send textField tag and textView tag, but custom (not indexPath.row only). 我尝试发送textField标签和textView标签,但是自定义(不仅仅是indexPath.row)。 because tag is Int, so I custom it like this: 因为tag是Int,所以我这样定制它:
let tagString = String(row) + String(703)
print("tagString:\(tagString)")
let intTagString = Int(tagString)
cell.txtNote.tag = intTagString!
when print I got custom tagString example like 0703, 1703, 2703 and etc. 打印时,我得到自定义tagString示例,如0703,1703,2703等。
But when I try to get txtNote tag with this : 但是当我尝试使用txtNote标签时:
func textViewDidChange(_ textView: UITextView) {
print("textview text:\(textView.text!) and text tag:\(textView.tag)")
}
text tag: only print last intTagString as 703 only. text tag:仅打印最后一个intTagString为703。
How to correct this so I can get whole custom tag? 如何纠正这一点,以便我可以得到整个自定义标签?
When you cast a literal integer string to integer, first zeros (0000...) will be omitted. 将文字整数字符串转换为整数时,将省略第一个零(0000 ...)。 That's why "00703"
becomes 703
这就是"00703"
成为703
的原因
You can correct this by adding a 'one' at the first place: "100703"
--> 100703
您可以通过在第一个位置添加“一个”来纠正此问题: "100703"
- > 100703
Edit. 编辑。 The whole solution using custom delegate: 使用自定义委托的整个解决方案:
import UIKit
class ViewController: UIViewController {
var tableView: UITableView!
let ReusedID = "**MyCell**"
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView.init(frame: view.bounds, style: .grouped)
tableView.register(MyCell.classForCoder(), forCellReuseIdentifier: ReusedID)
tableView.dataSource = self
tableView.delegate = self
view.addSubview(tableView)
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int { return 1 }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 8 }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: ReusedID, for: indexPath)
if let cell = cell as? MyCell {
cell.configCell(somethingHere: nil, parent: tableView, with: indexPath)
}
return cell
}
}
/// Create a custom delegate for tableview
protocol MyTableViewDalegate: UITableViewDelegate {
func tableView(_ tableView: UITableView?, didChange textView: UITextView, at indexPath: IndexPath)
}
/// [***] Implement the custom delegate
extension ViewController: MyTableViewDalegate {
func tableView(_ tableView: UITableView?, didChange textView: UITextView, at indexPath: IndexPath) {
print("table view cell did change with indexpath: ", indexPath)
}
}
/// Custom Cell Class which implement text view delegate
class MyCell: UITableViewCell, UITextViewDelegate {
var textView: UITextView!
var cellIndexPath: IndexPath!
var parent: UITableView?
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
textView = UITextView.init()
textView.delegate = self
textView.text = "Lorem ipsum sit dolor"
addSubview(textView)
textView.sizeToFit()
} // End of UI preparation
func configCell(somethingHere: Any?, parent: UITableView?, with indexPath: IndexPath) {
self.parent = parent
self.cellIndexPath = indexPath
}
// TextView Delegate
func textViewDidChange(_ textView: UITextView) {
let delegate = parent?.delegate as? MyTableViewDalegate
delegate?.tableView(parent, didChange: textView, at: cellIndexPath) // <== HERE
// The delegate will be called in [***] section above
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.