swift – Is Apple’s iOS documentation incorrect about beforehand registered class with the identical reuse identifier being changed with the brand new cellClass?

0
1
swift – Is Apple’s iOS documentation incorrect about beforehand registered class with the identical reuse identifier being changed with the brand new cellClass?


Apple’s documentation says that:

For those who beforehand registered a category or nib file with the identical reuse identifier, the category you specify within the cellClass parameter replaces the previous entry. It’s possible you’ll specify nil for cellClass if you wish to unregister the category from the required reuse identifier.

This does not appear to be right so far as I can inform.

Beneath easy pattern code demonstrates the problem. Principally, I’ve a slider which modifications the padding worth. When padding modifications, it ought to re-register (change the previous entry) the category with reuse identifier and reload the desk to indicate the brand new padding:

import UIKit
import SnapKit

extension String {
    static let kPadding = Self("padding")
    static let cellId = Self("cell")
}

class ViewController: UIViewController, UITableViewDataSource {
    
    let tableView = UITableView(body: .zero, type: .plain)

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        registerCell()
        tableView.dataSource = self
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.horizontalEdges.high.equalToSuperview()
        }
        
        let slider = UISlider()
        slider.isContinuous = false
        slider.minimumValue = 0
        slider.maximumValue = 100
        slider.worth = UserDefaults.commonplace.float(forKey: .kPadding)
        slider.addTarget(self, motion: #selector(sliderChanged(slider:)), for: .valueChanged)
        
        view.addSubview(slider)
        slider.snp.makeConstraints { make in
            let padding = 15.0
            make.horizontalEdges.backside.equalTo(view.safeAreaLayoutGuide).inset(padding)
            make.high.equalTo(tableView.snp.backside).offset(padding)
        }
        
    }
    
    @objc func sliderChanged(slider : UISlider) {
        print("sliderChanged: (slider.worth)")
        UserDefaults.commonplace.set(slider.worth, forKey: .kPadding)
        registerCell()
        tableView.reloadData()
    }
    
    func registerCell(){
        tableView.register(Cell.self, forCellReuseIdentifier: .cellId)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        100
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: .cellId) as! Cell
        
        cell.label.textual content = "Hiya (indexPath.row)"
        
        return cell
    }

}

class Cell: UITableViewCell {
    
    let label = UILabel()
    
    override init(type: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(type: type, reuseIdentifier: reuseIdentifier)
        label.font = UIFont.systemFont(ofSize: 34, weight: .daring)
        
        contentView.addSubview(label)
        label.snp.makeConstraints { make in
            make.edges.equalTo(UserDefaults.commonplace.float(forKey: .kPadding))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been applied")
    }
}

Nevertheless, this does not appear to be the case. It continues to make use of the earlier padding and previous entry will not be changed.

Am I misunderstanding what Apple’s documentation is saying?

LEAVE A REPLY

Please enter your comment!
Please enter your name here