Ice
Ice

Reputation: 757

Should I use prepareForReuse of UITableViewCell if the cell is built by UIContentConfiguration?

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?

Upvotes: 2

Views: 50

Answers (0)

Related Questions