Marlon Brando aka Ben
Marlon Brando aka Ben

Reputation: 933

What is the difference between when binding data to tableview cell iOS by using didSet method or using a function in iOS, Swift

  1. we can bind data to the UITableViewCell like below in didSet method.
class NameCell: UITableViewCell {

    @IBOutlet weak var nameLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        selectionStyle = .none
    }

    var name: String! {
        didSet {
            nameLabel.text = name
        }
    }
}

  1. Or we can bind the data using a function like below.
class NameCell: UITableViewCell {

    @IBOutlet weak var nameLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        selectionStyle = .none
    }

    func bindName(name: String) {
       nameLabel.text = name
    }
}

What is the difference between these two approaches when it comes to performance and reliability of the app with large chunk of data

Upvotes: -1

Views: 115

Answers (1)

Ankur Lahiry
Ankur Lahiry

Reputation: 2315

This varies from developer to developer. I prefer the first one if the code structures in didset is too small. But if you make some calculations and decisions then the second one is better to me. It actually depends on you, so there is no specific answer.

Upvotes: 1

Related Questions