当您将文字整数字符串转换为整数时,将省略第一个零 (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
}
}