简体   繁体   English

创建自定义tableView单元格textField或textView标记

[英]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.

相关问题 如何在tableView之外获取所有自定义tableview单元格的textField和textView值 - How to get all custom tableview cell textField and textView value outside tableView 在自定义单元格TableView中选择了哪个TextField? - Which TextField was selected in custom-Cell TableView? 如何在tableView中创建自定义单元格(滑动单元格) - how to create custom cell in tableView (swipe cell) 当Ui Tableview滚动时,单元格文本内的自定义文本字段为空 - Custom textfield inside a cell text is blank when Ui Tableview Scrolled 无法在TableView自定义单元格中获取文本字段文本 - Can't get the textfield text in tableview custom cell 在表视图的自定义单元格上更改文本字段文本的突出显示颜色 - Changing the highlighted color of textfield text on the custom cell of a tableview 无法在自定义tableView单元格的textview中设置文本 - Cannot set text in textview in my custom tableView cell 如何在tableview单元格(每行)中添加文本字段并将标记设置到每个文本字段以访问其文本 - how to add textfield in tableview cell(each row) and set tag to each textfield to access it's text Swift 在 UIAlertviewcontroller 中使用自定义单元格创建 tableview - Swift create tableview with custom cell within UIAlertviewcontroller 如何在“自定义表格视图”单元格下创建表格视图 - How can I Create a Tableview Under Custom Tableview cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM