gpt4 book ai didi

ios - 为什么调用 reloadRows 后在数组中找不到 heightForRowAt 值?

转载 作者:行者123 更新时间:2023-11-30 11:51:45 29 4
gpt4 key购买 nike

为什么函数 heightForRowAt 总是找到包含行高的数组,该数组最初设置为 nil,即使在重新加载行后仍然为零。

    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]
}
}

更新的 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
}

}

最佳答案

您的代码存在大量问题

1-首先您必须下载、保存图像,然后重新加载表格

2-秒你应该使用自动表格 View 高度来实现这一点,而不是像你当前所做的那样使用高度数组

解决1

   NSString *getImagePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpeg",n1.Id]];

if([[NSFileManager defaultManager] fileExistsAtPath:getImagePath])
{
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
cell.leftImageV.image =img;
[cell.activity stopAnimating];
cell.activity.hidden=YES;
}
else
{

[cell.activity startAnimating];
cell.activity.hidden=NO;

cell.leftImageV.image = nil;

NSLog(@"xdasdxsadxa %@",n1.mainImgStr);

dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(concurrentQueue, ^{
__block NSData * imageData = nil;
dispatch_sync(concurrentQueue, ^{

imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:n1.mainImgStr]];

//Add the file name
[imageData writeToFile:getImagePath atomically:YES]; //Write the file

});

dispatch_sync(dispatch_get_main_queue(), ^{

if(imageData)
{
[self.tableView reloadData];

}
});
});

解决2

要设置行高和估计行高的自动尺寸,请确保执行以下步骤以使自动尺寸对单元格/行高布局有效。我刚刚测试了以下步骤和代码并且工作正常。

  • 分配并实现表格 View 数据源和委托(delegate)
  • UITableViewAutomaticDimension分配给rowHeight和estimatedRowHeight
  • 实现委托(delegate)/数据源方法(即 heightForRowAt 并向其返回一个值 UITableViewAutomaticDimension)

-

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

// Don't forget to set dataSource and delegate for table
table.dataSource = self
table.delegate = self

// Set automatic dimensions for row height
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension
}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

关于ios - 为什么调用 reloadRows 后在数组中找不到 heightForRowAt 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48268529/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com