首页 > 解决方案 > 插入 Firestore 数据时图像重复

问题描述

我在表格视图中创建了一个小的“通知中心”。但不知何故,图像正在重演,这不是它应该的方式。

我自己也搞不清楚是什么原因,所以请大家帮忙。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let listPath = list[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "ListCell") as! HistoryCell
    cell.setCell(list: listPath) 

    print(cell.note.text!)

     //THIS ONE
    if cell.note.text!.isEmpty == false
    {
        cell.noteImg.isHidden = false
    }


    return cell
}

这是我的部分代码。所以让我解释一下这实际上是如何工作的。

每次用户将数据添加到特定文本字段时,都应将其添加到 Firestore 的“注释”中。我从 Firestore 中选择所有内容以将数据插入到 thisUITableView中,并且如果“notes”不为空。我的图像应该是可见的。

我现在得到的结果:即使值为 nil,它也会重复添加。

图像

所以你可以在图片上看到。第一个(1)是正确的。(12:55 的那个)但第二个不应该在那里,它只是随机添加的。

我真的希望你能理解,保持简短和有条理并不容易。


编辑:

 func setCell(list: listTxt){
    DogName.text = list.dog
    Person.text = list.person
    Action.text = list.action
    Date.text = list.date
    Time.text = list.time
    timer.text = list.timer
    kilometer.text = list.kilometers
    documentID.text = list.documentID
    latitude = list.latitude
    longitude = list.longitude
    note.text = list.note
    noteImg.image = list.noteImg
}

标签: iosswiftgoogle-cloud-firestore

解决方案


我敢肯定,但根据我的知识和经验,下面的代码会导致意外输出。

cell.note.text!.isEmpty == false

UITableView正在填充数据reusingUITableViewCell这就是为什么你会得到意想不到的输出。

只需cellForRowAt用我的代码替换方法即可。我希望它会奏效。

代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let listPath = list[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: "ListCell") as! HistoryCell
    cell.setCell(list: listPath) 

    if listPath.note.isEmpty == false{
        cell.noteImg.isHidden = false
    }else{
        cell.noteImg.isHidden = true
    }
    return cell
}

更新:

dequeueReusableCell(withIdentifier:for:)

返回指定重用标识符的可重用表视图单元对象并将其添加到表中。

参考:dequeueReusableCell


推荐阅读