ios - 插入 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
}
解决方案
我敢肯定,但根据我的知识和经验,下面的代码会导致意外输出。
cell.note.text!.isEmpty == false
UITableView
正在填充数据reusing
。UITableViewCell
这就是为什么你会得到意想不到的输出。
只需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:)
返回指定重用标识符的可重用表视图单元对象并将其添加到表中。
推荐阅读
- angular - TypeScript getter/setter 中的部分
- javascript - Globalize.js - cldr.once 不是一个函数
- javascript - 将命名函数绑定到上下文
- c# - 如何防止查询字符串httpclient的双重编码?
- homebrew - Flutter 说没有安装 Brew
- android - Android 区分电视和机顶盒
- unity3d - 四元数校正
- spring-security - Gatling 测试 CSRF Spring Security 通过 Web 表单阻止了我的帖子
- android - Android Studio 无法识别 .kt 文件
- c# - 检查负数是否在一个范围内