【问题标题】:Image repeats when inserting Firestore data插入 Firestore 数据时图像重复
【发布时间】:2019-06-05 19:48:05
【问题描述】:

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

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

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 中选择所有内容以将数据插入到此 UITableView 中,并且如果“注释”不为空。我的图像应该是可见的。

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

正如您在图片中看到的那样。 第一个(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
}

【问题讨论】:

  • 请贴出setCell方法的内容
  • @iMHiteshSurani 你去吧

标签: ios swift google-cloud-firestore


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 2013-11-10
  • 2018-10-23
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
相关资源
最近更新 更多