如何阻止TableViewCell重复图像

时间:2021-05-01 20:33:52

I'm importing contacts into my application and inserting them into a tableview.

我正在将联系人导入我的应用程序并将其插入到tableview中。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: importCellId, for: indexPath) as! tbcImportContact
    cell.contact = contacts[indexPath.row]
    return cell
}

When I set a contacts image like I am doing here:

当我设置联系人图像时,就像我在这里做的那样:

var contact: Contact? {
    didSet {
        if let name = contact?.name {
            nameLabel.text = name
        } else {
            nameLabel.text = ""
        }
        if let img = contact?.contactImage {
            contactImage.image = img
        } else {
            contactImage.image = UIImage(named: "emptyProfile")
        }
    }

The next contact with a nil image should receive the UIImage(named: "emptyProfile") but instead the contact is assigned the previous contacts image.

与nil图像的下一次接触应该接收UIImage(命名为“emptyProfile”),而是为联系人分配先前的联系人图像。

I have tried adding that else statement to prevent this but it doesn't work. I have also tried adding:

我已经尝试添加else语句来防止这种情况,但它不起作用。我也尝试过添加:

override func prepareForReuse() {
    self.contactImage.image = UIImage(named: "emptyProfile")
    self.nameLabel.text = ""
}

to reset the cells when assigning a contact to it but that also did not work. I also tried setting it async on the main thread and that also did not work.

在为其分配联系人时重置单元格但是也不起作用。我也尝试在主线程上设置async,但也没有用。

Any suggestions on what else I can try to fix this?

关于我还可以尝试解决此问题的任何建议?

1 个解决方案

#1


1  

I ended up just setting the image for the cell when i import the contacts like this:

当我导入这样的联系人时,我最终只是设置了单元格的图像:

  if let imageData = contact.imageData {
                    image = UIImage(data: imageData)
                } else {
                    image = UIImage(named: "contactProfile")
                }

here is the code on how i import the contacts:

这是我如何导入联系人的代码:

do {
        let keysToFetch = keys as [CNKeyDescriptor]
        try contactsStore.enumerateContacts(with: CNContactFetchRequest(keysToFetch: keysToFetch)) {
            (contact, cursor) -> Void in
            for phoneNumber in contact.phoneNumbers {
                let phoneNumberString = phoneNumber.value.stringValue
                if let imageData = contact.imageData {
                    image = UIImage(data: imageData)
                } else {
                    image = UIImage(named: "contactProfile")
                }

                let emailAddress = contact.emailAddresses.count > 0 ? contact.emailAddresses[0].value : ""
                let postalAddress = contact.postalAddresses.count > 0 ? "\(contact.postalAddresses[0].value)" : ""                    
                self.contacts.append(Contact(name: contact.givenName, phoneNumber: phoneNumberString, contactImage: image, email: emailAddress as String , address: postalAddress))
            }
        }

        if contacts.count == 0 {
            message = "No contacts found."
        }
    } catch {
        message = "Unable to fetch contacts."
    }

#1


1  

I ended up just setting the image for the cell when i import the contacts like this:

当我导入这样的联系人时,我最终只是设置了单元格的图像:

  if let imageData = contact.imageData {
                    image = UIImage(data: imageData)
                } else {
                    image = UIImage(named: "contactProfile")
                }

here is the code on how i import the contacts:

这是我如何导入联系人的代码:

do {
        let keysToFetch = keys as [CNKeyDescriptor]
        try contactsStore.enumerateContacts(with: CNContactFetchRequest(keysToFetch: keysToFetch)) {
            (contact, cursor) -> Void in
            for phoneNumber in contact.phoneNumbers {
                let phoneNumberString = phoneNumber.value.stringValue
                if let imageData = contact.imageData {
                    image = UIImage(data: imageData)
                } else {
                    image = UIImage(named: "contactProfile")
                }

                let emailAddress = contact.emailAddresses.count > 0 ? contact.emailAddresses[0].value : ""
                let postalAddress = contact.postalAddresses.count > 0 ? "\(contact.postalAddresses[0].value)" : ""                    
                self.contacts.append(Contact(name: contact.givenName, phoneNumber: phoneNumberString, contactImage: image, email: emailAddress as String , address: postalAddress))
            }
        }

        if contacts.count == 0 {
            message = "No contacts found."
        }
    } catch {
        message = "Unable to fetch contacts."
    }