Problem: See video. I'm downloading the images from Firebase storage and displaying on the table view cell. After I build the app, the images don't show up in the first cells of the table view. Upon scrolling down and back up, I can see images in first cells of the table view.
I consulted this question (clearing app data & cache of simulator didn't work) and this question (different problem b/c they're downloading image from url). I read this about using a placeholder image, but I'm not sure if it's applicable because I'm not using SDWebImage and my cell uses my own companyImage
UIImageView
and not the imageView
that comes with UITableView
cell.
Code:
Company View Controller:
class CompanyViewController: UIViewController, UISearchBarDelegate, UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
companyTableView.register(CompanyTableViewCell.self, forCellReuseIdentifier: "CompanyTableViewCell")
companyTableView.register(UINib(nibName: "CompanyTableViewCell", bundle: nil), forCellReuseIdentifier: "CompanyTableViewCell")
loadData()
}
func loadData() {
databaseHandle = databaseRef?.child("companies").observe(.value, with: { (snapshot) in
for item in snapshot.children.allObjects as! [FIRDataSnapshot] {
let company = Company()
let id = item.childSnapshot(forPath: Property.id.rawValue).value as! String
let imageName = id + ".png"
let imageRef = self.storageRef?.child(imageName)
imageRef?.data(withMaxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print((error as Error).localizedDescription)
} else if let data = data {
// Data for "images/companyid.png" is returned
company.image = UIImage(data: data)
}
}
self.informationStateController?.addCompany(company: company)
}
DispatchQueue.main.async {
self.companyTableView.reloadData()
}
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let company = (self.informationStateController?.companies[indexPath.row])!
let customCell = tableView.dequeueReusableCell(withIdentifier: CompanyTableViewCell.identifier) as! CompanyTableViewCell
customCell.companyImage.image = company.image
return customCell
}
}
Company Table View Cell
class CompanyTableViewCell: UITableViewCell {
@IBOutlet weak var companyImage: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
companyImage.contentMode = .scaleAspectFit
}
}