Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MultilineLabelCell: UITableViewCell {
- let label = UILabel()
- override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
- super.init(style: style, reuseIdentifier: reuseIdentifier)
- commonInit()
- }
- required init?(coder: NSCoder) {
- super.init(coder: coder)
- commonInit()
- }
- private func commonInit() {
- label.translatesAutoresizingMaskIntoConstraints = false
- contentView.addSubview(label)
- let g = contentView.layoutMarginsGuide
- NSLayoutConstraint.activate([
- label.topAnchor.constraint(equalTo: g.topAnchor),
- label.leadingAnchor.constraint(equalTo: g.leadingAnchor),
- label.trailingAnchor.constraint(equalTo: g.trailingAnchor),
- label.bottomAnchor.constraint(equalTo: g.bottomAnchor),
- ])
- label.numberOfLines = 0
- // so we can see the framing
- label.backgroundColor = .yellow
- }
- }
- class ReloadRowsTestVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
- var myData: [String] = []
- var reloadCount: Int = 0
- let tableView = UITableView()
- override func viewDidLoad() {
- super.viewDidLoad()
- // some sample data
- for i in 1..<50 {
- let nLines = i % 4
- var s: String = "Row: \(i-1)\nLine 1"
- for n in 0..<nLines {
- s += "\nLine \(n+2)"
- }
- myData.append(s)
- }
- myData[4] += "\nReload Count: 0"
- var cfg = UIButton.Configuration.filled()
- cfg.title = "Reload row 4"
- let btn = UIButton(configuration: cfg, primaryAction: UIAction() { _ in
- self.reloadCount += 1
- self.myData[4] += "\nRow 4 - Reload Count: \(self.reloadCount)"
- print("Reload Count:", self.reloadCount)
- self.tableView.reloadRows(at: [IndexPath(row: 4, section: 0)], with: .none)
- })
- btn.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(btn)
- tableView.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(tableView)
- let g = view.safeAreaLayoutGuide
- NSLayoutConstraint.activate([
- btn.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
- btn.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40.0),
- btn.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -40.0),
- tableView.topAnchor.constraint(equalTo: btn.bottomAnchor, constant: 20.0),
- tableView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
- tableView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
- tableView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
- ])
- tableView.register(MultilineLabelCell.self, forCellReuseIdentifier: "c")
- tableView.dataSource = self
- tableView.delegate = self
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return myData.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- print("cellForRowAt:", indexPath)
- let c = tableView.dequeueReusableCell(withIdentifier: "c", for: indexPath) as! MultilineLabelCell
- c.label.text = myData[indexPath.row]
- return c
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement