0

I have a tableview called (events) where i am pulling images from firebase and loading them, The idea is to post events and add flyer images with the event post. it works perfectly but when i close the app and reopen the images are not in the correct order and some images are on the wrong event. Any help will be greatly appreciated i am very new to coding.

        eventsDatabaseHandle = eventsRef?.child("Church Events").observe(.childAdded, with: { (snaphot) in
            let eventPost = snaphot.value as! [String: Any]
            var event = Event(title: eventPost["eventtitle"] as! String,
                              timestamp: eventPost["eventdate"] as! String,
                              location: eventPost["eventlocation"] as! String,
                              image: nil)

            let task = URLSession.shared.dataTask(with: URL(string: eventPost["ImageUrl"] as! String)!) { data, _, error in

                if let image: UIImage = UIImage(data: data!) {
                    event.image = image
                    DispatchQueue.main.async {
                        self.events.append(event)
                        self.tableView.reloadData()
                    }
                }
            }
            task.resume()

        })


}


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return events.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "events", for: indexPath) as! EventsTableViewCell
        let event = events[indexPath.row]
        cell.flyerImages.image = event.image
        cell.eventTitle.text = event.title
        cell.eventDate.text =  event.timestamp
        cell.eventLocation.text = event.location
Neil Leon
  • 55
  • 6
  • You may want to try [this](https://stackoverflow.com/a/35958896/6532217) approach – Ali Apr 15 '20 at 22:05
  • Hi @AliZahr thank you for your response, are you able to show me what that would look like with my code? – Neil Leon Apr 15 '20 at 22:12
  • Right after you initialize the event, dont download the image, just append to the array and reload the table. In the cellForRowAtIndexPath download the image using the library explained in the answer – Ali Apr 15 '20 at 22:17
  • Hi @Ali I tried to make you recommendation work but I can’t figure it out or it’s not working. Like I said I’m really new to swift. – Neil Leon Apr 19 '20 at 00:07

1 Answers1

1

Edit your Event object to take imageURL instead of UIImage, and try the following after download this lightweight library which handles downloading and caching your images

    eventsDatabaseHandle = eventsRef?.child("Church Events").observe(.childAdded, with: { (snaphot) in
        let eventPost = snaphot.value as! [String: Any]
        var event = Event(title: eventPost["eventtitle"] as! String,
                          timestamp: eventPost["eventdate"] as! String,
                          location: eventPost["eventlocation"] as! String,
                          imageURL: eventPost["ImageUrl"])

         self.events.append(event)
    })

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "events", for: indexPath) as! EventsTableViewCell
    let event = events[indexPath.row]

    if let imgURL = URL(string:event.imageURL)
    {
        cell.flyerImages.kf.setImage(with:imgURL)
    }
    cell.eventTitle.text = event.title
    cell.eventDate.text =  event.timestamp
    cell.eventLocation.text = event.location
}
Ali
  • 497
  • 1
  • 4
  • 16