gpt4 book ai didi

ios - 为什么重新加载行不能正确调整 TableView 行的大小?

转载 作者:行者123 更新时间:2023-11-28 15:06:59 26 4
gpt4 key购买 nike

tableViews 行高未正确调整自身大小,我试图根据下面的纵横比调整 tableViews 行高的大小,这是我目前为 cellForRowAtheightForRowAt。还有一些图片被下载了两次,而另一些则根本没有下载。

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 let image = imageCache.object(forKey: self.imageURLS[indexPath.row] as NSString)
{
cell.cellImageView.image = image
}
else{
URLSession.shared.dataTask(with: downloadURL!, completionHandler: {(data,response,error) in

if error != nil {
print(error!)
return
}
let downloadedImage = UIImage(data:data!)
let aspect = CGFloat((downloadedImage?.size.width)!/(downloadedImage?.size.height)!)
self.imageCache.setObject(downloadedImage!, forKey: self.imageURLS[indexPath.row] as NSString)
DispatchQueue.main.async {
cell.cellImageView.image = downloadedImage
cell.cellImageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width/aspect)
cell.imageView?.contentMode = .scaleAspectFit
// cell.cellImageView.heightAnchor.constraint(equalTo: cell.cellImageView.widthAnchor, multiplier: aspect)
tableView.reloadRows(at: [indexPath], with: .top)
}
}).resume()
}

return cell
}



override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

根据建议更新代码

    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 let image = imageCache.object(forKey: self.imageURLS[indexPath.row] as NSString)
{
cell.cellImageView.image = image
}
else{
URLSession.shared.dataTask(with: downloadURL!, completionHandler: {(data,response,error) in

if error != nil {
print(error!)
return
}
let downloadedImage = UIImage(data:data!)
let aspect = CGFloat((downloadedImage?.size.width)!/(downloadedImage?.size.height)!)
self.imageCache.setObject(downloadedImage!, forKey: self.imageURLS[indexPath.row] as NSString)
DispatchQueue.main.async {
cell.cellImageView.image = downloadedImage
cell.cellImageView.heightAnchor.constraint(equalTo: cell.cellImageView.widthAnchor, multiplier: aspect).isActive = true
cell.imageHeight.constant = (cell.bounds.width * (cell.cellImageView.image?.size.height)!)/(cell.cellImageView.image?.size.width)!
cell.layoutIfNeeded()
tableView.reloadRows(at: [indexPath], with: .top)
}
}).resume()
}

return cell
}



override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
print("UITableViewAutomaticDimension: \(UITableViewAutomaticDimension)")
return UITableViewAutomaticDimension
}
}

最佳答案

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-关于纵横比,拖拽imageView的高度为IBOutlet

并在 cellForRowAt 中执行此操作

   cell.imageHeightCon.constant = (cellWidth*imageRealHeight)/imageRealWidth

注意:这里我假设 imageview 宽度等于单元格宽度,最好给它 tableViewWidth 与 viewController 的 View 成比例,因为这里的 cellWidth 还没有呈现,说 tableView 需要全屏宽度然后设置cellWidth 到 self.view.frame.size.width

3- 不要忘记将此行放在返回单元格之前

   cell.layoutIfNeeded()

关于ios - 为什么重新加载行不能正确调整 TableView 行的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48288498/

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