Reputation: 328
I've created a small "notification-center" in a tableview. But somehow the image is repeating itself, and that's not the way it should be.
I can't really figure out the reason for this by my own, so I'm asking you guys for some help.
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
}
This is parts of the code I've. So let me explain how this actually works.
Every time a user added data to a SPECIFIC textfield it should be added in "notes" in Firestore.
I select everything from Firestore to insert the data into this UITableView
, and IF "notes" is not empty. My image should be visible.
The result I get now: It will add repeatedly even with value is nil.
So as you can see on the picture. The first (1) is correct. (The one at 12:55) but the second one should NOT be there, it's just added randomly.
I really hope you understand, wasn't too easy to keep it short and organized.
Edit:
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
}
Upvotes: 1
Views: 47
Reputation: 13567
I am sure but according to my knowledge and experience below code cause unexpected output.
cell.note.text!.isEmpty == false
UITableView
is populating data by reusing
the UITableViewCell
. That's why you are getting unexpected output.
Just replace cellForRowAt
method with my code. I hope it will work.
Code:
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
}
Returns a reusable table-view cell object for the specified reuse identifier and adds it to the table.
Reference: dequeueReusableCell
Upvotes: 2