1

I try to use the new Architecture for the building cell of UITableView, but I can't understand whether should I use prepareForReuse for this case, because I don't understand how

My cell :

class CustomTableViewCell: UITableViewCell {

static let identifier = "CustomTableViewCell"

override func updateConfiguration(using state: UICellConfigurationState) {
    super.updateConfiguration(using: state)

    var backgroundConfig = backgroundConfiguration?.updated(for: state)
    backgroundConfig?.backgroundColor = .white
    
    if state.isHighlighted || state.isSelected {
        backgroundConfig?.backgroundColor = .lightGray
    }
    
    backgroundConfiguration = backgroundConfig
}

}

My Configurator:

protocol MyContentConfigurationProtocol: AnyObject {
    func userPressButton(state: ButtonState, index: Int)
}

struct MyContentConfiguration : UIContentConfiguration {
    
    var buttonState: ButtonState = .Download
    var index = 0
    var name = "Name of Data"
    var progress: Float = 0.0
    
    weak var delegate: MyContentConfigurationProtocol?
    
    func makeContentView() -> UIView & UIContentView {
        return MyContentView(configuration:self)
    }
    func updated(for state: UIConfigurationState) -> MyContentConfiguration {
        return self
    }
}

Content View

class MyContentView : UIView, UIContentView {
    var configuration: UIContentConfiguration {
        didSet {
            config()
        }
    }

....
    init(configuration: UIContentConfiguration) {
        self.configuration = configuration
        super.init(frame:.zero)
....
        // 2
        config()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func config() {
        guard let config = getConfig() else {
            print("Nil state")
            return
        }
        
        state = config.buttonState
        index = config.index
        setButtonsState(state: config.buttonState)
        nameOfData.text = config.name
        setProgressView(state: config.buttonState, progress: config.progress)
        
    }
    
    func getConfig() -> (MyContentConfiguration?) {
        return (configuration as? MyContentConfiguration)
    }
    
    @objc func userPressDownload(_ sender: UIButton) {
        guard let config = getConfig() else {
            print("Nil state")
            return
        }
        config.delegate?.userPressButton(state: .Resume, index: index)
    }
    
    @objc func userPressCancel(_ sender: UIButton) {
        guard let config = getConfig() else {
            print("Nil state")
            return
        }
        config.delegate?.userPressButton(state: .Download, index: index)
        
        
    }
}

And my cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.identifier, for: indexPath)
    
        var config = MyContentConfiguration()
        config.buttonState = listOfData[indexPath.row].state
        config.progress = listOfData[indexPath.row].progress
        config.index = indexPath.row
        config.delegate = self
        cell.contentConfiguration = config
        return cell
    }

How in this case use prepareForReuse and this method is necessary for this new Architecture approach?

Ice
  • 680
  • 1
  • 10
  • 23
  • `prepareForReuse` was _never_ "necessary". I've been doing iOS from the beginning and I've never used it. – matt Sep 26 '22 at 19:39
  • @matt I got you, but not agree with you, because, in the case of downloading images to cell by async it is necessary. In this architecture case, I can't understand the need for this method, should I worry about this method or about it worry iOS – Ice Sep 27 '22 at 07:16
  • "in the case of downloading images to cell by async" I don't use `prepareForReuse` for that either. And I don't see the "downloading images" part of your code. – matt Sep 27 '22 at 09:07
  • @matt from documentation about prepareForReuse : To avoid potential performance issues, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state. In this case, should I worry about it or this architecture approach handle it without me – Ice Sep 27 '22 at 10:45

0 Answers0