Why does the function heightForRowAt
always find the array containing row heights which is initially set to nil to sill be nil even after reloading the rows.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Reuse", for: indexPath) as! TableViewCell
let downloadURL = URL(string: self.imageURLS[indexPath.row])
if cell.cellImageView.image == nil{
URLSession.shared.dataTask(with: downloadURL!) { (data, _, _) in
if let data = data {
let image = UIImage(data: data)
DispatchQueue.main.async {
cell.setCellImage(image:image!)
self.rowHeights?.insert((cell.imageView?.frame.height)!, at: indexPath.row)
tableView.reloadRows(at: [indexPath], with: .top)
}
}
}.resume()
}
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
guard let rowHeights = self.rowHeights else{
print("returned nil")
return 500.0
}
print("the array exists at \(indexPath.row) with value: \(rowHeights[indexPath.row])")
return rowHeights[indexPath.row]
}
}
UPDATED TableViewController
import UIKit
import Firebase
import FirebaseStorageUI
class TableViewController: UITableViewController {
var images:[UIImage]! = [#imageLiteral(resourceName: "rininger_2.jpg")]
var imageURLS:[String] = [String]()
var rowHeights:[CGFloat] = [CGFloat]()
var listener:ListenerRegistration?
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
listener = Firestore.firestore().collection("Posts").addSnapshotListener{
querySnapshot, error in
guard let snapshot = querySnapshot else {
print("Error fetching snapshots: \(error!)")
return
}
snapshot.documentChanges.forEach { diff in
if (diff.type == .added) {
print("New data: \(diff.document.data())")
}
if (diff.type == .modified) {
print("Modified data: \(diff.document.data())")
}
if (diff.type == .removed) {
print("Removed data: \(diff.document.data())")
}
guard let newImageURL = diff.document.data()["imageDownloadURL"] as? String else{
print("Failed to get image download URL")
return
}
print("downloadURL: \(newImageURL)")
self.imageURLS.insert(newImageURL, at: 0)
let indexPath = IndexPath(row: 0, section: 0)
self.tableView.insertRows(at: [indexPath], with: .top)
}
}
tableView.estimatedRowHeight = 100.0
tableView.rowHeight = UITableViewAutomaticDimension
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return imageURLS.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Reuse", for: indexPath) as! TableViewCell
let downloadURL = URL(string: self.imageURLS[indexPath.row])
if cell.cellImageView.image == nil{
URLSession.shared.dataTask(with: downloadURL!) { (data, _, _) in
if let data = data {
let image = UIImage(data: data)
self.images.insert(image, at: indexPath.row)
DispatchQueue.main.async {
cell.setCellImage(image:image!)
// self.rowHeights.insert((cell.imageView?.frame.height)!, at: indexPath.row)
//tableView.reloadData()
tableView.reloadRows(at: [indexPath], with: .top)
}
}
}.resume()
}
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
print("UITableViewAutomaticDimension: \(UITableViewAutomaticDimension)")
return UITableViewAutomaticDimension
}
}