diff --git a/Sources/KNContactsPicker/KNContactCell.swift b/Sources/KNContactsPicker/KNContactCell.swift index e19c8e1..0082778 100644 --- a/Sources/KNContactsPicker/KNContactCell.swift +++ b/Sources/KNContactsPicker/KNContactCell.swift @@ -107,12 +107,12 @@ class KNContactCell: UITableViewCell { override func prepareForReuse() { super.prepareForReuse() self.disabled = false + self.isSelected = false } override func setSelected(_ selected: Bool, animated: Bool) { if (!disabled) { super.setSelected(selected, animated: animated) - self.setAppropriateStyle() } } @@ -176,6 +176,8 @@ class KNContactCell: UITableViewCell { } public func set(contactModel: KNContactCellModel) { + let isSelected = contactModel.getIsSelected() + self.setSelected(isSelected, animated: false) self.nameLabel.text = contactModel.getName() self.subtitleLabel.text = contactModel.getSubtitle() diff --git a/Sources/KNContactsPicker/KNContactCellModel.swift b/Sources/KNContactsPicker/KNContactCellModel.swift index 6872e00..72d039e 100644 --- a/Sources/KNContactsPicker/KNContactCellModel.swift +++ b/Sources/KNContactsPicker/KNContactCellModel.swift @@ -14,11 +14,17 @@ struct KNContactCellModel { private var contact: CNContact private var settings: KNPickerSettings private var formatter: CNContactFormatter + private var isSelected: Bool - init(contact: CNContact, settings: KNPickerSettings, formatter: CNContactFormatter) { + init(contact: CNContact, settings: KNPickerSettings, formatter: CNContactFormatter, isSelected: Bool) { self.contact = contact self.settings = settings self.formatter = formatter + self.isSelected = isSelected + } + + func getIsSelected() -> Bool { + return isSelected } func getName() -> String { diff --git a/Sources/KNContactsPicker/KNContactsPickerController.swift b/Sources/KNContactsPicker/KNContactsPickerController.swift index 742ebcd..0586895 100644 --- a/Sources/KNContactsPicker/KNContactsPickerController.swift +++ b/Sources/KNContactsPicker/KNContactsPickerController.swift @@ -147,15 +147,13 @@ class KNContactsPickerController: UITableViewController { override open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: CELL_ID, for: indexPath) as! KNContactCell let contact = self.getContact(at: indexPath) - let contactModel = KNContactCellModel(contact: contact, settings: settings, formatter: formatter) - - let disabled = ( shouldDisableSelection && !selectedContacts.contains(contact) ) || settings.conditionToDisableContact(contact) - let selected = selectedContacts.contains(contact) + let disabled = ( shouldDisableSelection && !selectedContacts.contains(contact) ) || settings.conditionToDisableContact(contact) + let contactModel = KNContactCellModel(contact: contact, settings: settings, formatter: formatter, isSelected: selected) + cell.set(contactModel: contactModel) - cell.setDisabled(disabled: disabled) - cell.setSelected(selected, animated: false) + cell.setAppropriateStyle() return cell }