【问题标题】:Create custom tableView cell textField or textView tag创建自定义 tableView 单元格 textField 或 textView 标签
【发布时间】:2019-05-13 02:28:29
【问题描述】:

我有多个tableView,每个tableView我都有txtName和txtNote。 txtName 是 textField,txtNote 是 textView。我尝试发送 textField 标签和 textView 标签,但自定义(不是 indexPath.row 而已)。因为标签是 Int,所以我这样自定义:

let tagString = String(row) + String(703)
        print("tagString:\(tagString)")
        let intTagString = Int(tagString)
        cell.txtNote.tag = intTagString!

打印时,我得到了自定义 tagString 示例,例如 0703、1703、2703 等。

但是当我尝试用这个获取 txtNote 标记时:

func textViewDidChange(_ textView: UITextView) {
    print("textview text:\(textView.text!) and text tag:\(textView.tag)")
}

文本标签:仅将最后一个 intTagString 打印为 703。

如何纠正这个问题,以便我可以得到整个自定义标签?

【问题讨论】:

  • 你在哪里为 textView 设置标签? & 你的意思是 tableView 有多个单元格,每个单元格都有 txtName 和 txtNote?
  • 我从情节提要中设置标签。是的,每个tableView都有txtName和txtNote。不是多个单元格,而是多个 tableView。

标签: ios swift uitableview tags textfield


【解决方案1】:

当您将文字整数字符串转换为整数时,将省略第一个零 (0000...)。这就是为什么"00703" 变成703

您可以通过在第一个位置添加“一个”来纠正此问题:"100703" --> 100703


编辑。使用自定义委托的整个解决方案:

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
    }
}

【讨论】:

  • 或者切换成String(703) + String(row)?
  • 是的,您喜欢的每条规则。但是,如果您想稍后使用这些标记值做某事,则需要应用您的规则来过滤您想要的数字。
  • 稍后,我从该 Int 创建字符串并分隔最后一个字符,因为我设置的最后一个字符永远不会超过 9。所以它只有一个字符永远不会像 10 那样加倍等等。所以我得到了哪个 tableView 和我得到正在编辑的行。
  • 你为什么不为此苹果delegate?处理您的案件更清晰、更安全。
  • 所以,我尝试从单元格换行
猜你喜欢
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多