- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么函数 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
要设置行高和估计行高的自动尺寸,请确保执行以下步骤以使自动尺寸对单元格/行高布局有效。我刚刚测试了以下步骤和代码并且工作正常。
UITableViewAutomaticDimension
分配给rowHeight和estimatedRowHeightheightForRowAt
并向其返回一个值 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/
使用此代码,我根据数组中的项目数设置单元格的自动尺寸。它可以工作,但是当我使用另一个 viewController 添加一些新项目然后返回到此时,即使数组数量增加,高度也不会更新。 我在viewDid
像往常一样,我尝试使用以下代码向某些单元格添加一些自定义高度值: func tableView(_ tableView: UITableView, heightForRowAt indexPath:
我正在开发一个电子商务应用程序,但在尝试设置特定行的高度时遇到了问题。当我选择类别时,表格会重新加载,并且根据我选择的类别,或多或少的行会通过 api。 因此,使用此设置,当我选择一个特定类别时它可以
我有一个带有自定义 UITableViewCell 的 UITableViewController。每个单元格有 2 个标签。当单元格被选中时,它会扩展到一个固定值,我通过 tableView(_ t
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if le
我正在尝试根据下载图像的大小实现动态大小的行高。我遇到的问题是当函数 heightForRowAt 运行时图像没有被下载。实现此代码的正确方法是什么。 images是UIImage的数组,rowHei
我正在研究 UI 以使其与设计相同,我使用 heightForRowAt 将第一部分的单元格高度设置为 96px,我尝试了: override func tableView(_ tableView:
我正在使用 UITableViewController 并将 UICollectionView 放在 TableView 单元格的第二行中。问题是我如何动态获取 heightForRowAt 。 我尝
我有一个 View Controller ,它使用名为 searchCell 的自定义单元格来引入内容,我想知道如何获取标签 lblLeft 的高度并在 tableView(_ tableView:
什么时候设置改变 heightForRowAt UITableView 动画是 broken cell 是一个跳跃。如果选择最后一行并滚动到顶部,然后点击折叠行,表格会向上跳。动画坏了。 overri
通常当我想设置行高时再设置 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFl
为什么函数 heightForRowAt 总是找到包含行高的数组,该数组最初设置为 nil,即使在重新加载行后仍然为零。 override func tableView(_ tableView
我有一个整数数组,我试图将数组中所有行的 UITableViewCell 高度设置为 50,并将其余行设置为 0。使用 Swift 3,我的数组返回 [0,1,3,6] 并且我使用 for 循环遍历数
我在一个 ViewController 中有 2 个 tableview。两个表格 View 的单元格高度不同。 在第一个表格 View 中,我通过 heightForRowAt 委托(delegat
我尝试为动态内容设置单元格高度。在单元格中,我使用了两个 UILabel,但我无法从 tableView(_ tableView: UITableView, heightForRowAt indexP
我正在更改每个 TableViewCell 的大小: override func tableView(_ tableView: UITableView, heightForRowAt indexPat
我有一个带有 UITableView 的 View Controller 。使用 RxSwift 填充表数据: let observable = Observable.just(data) obser
每当我重新加载 tableView 时,heightForRowAt 都会在 cellForRowAt 之后调用。它遵循的顺序是: numberofsections -> numberOfRowsI
我对编码还很陌生,所以尝试理解这些概念。 这里我试图获取具有动态单元格高度的 UITableView 的高度。 这是我的代码部分。 override func viewWillAppear(_ ani
我的tableView设置 appTableView.estimatedRowHeight = 200 appTableView.rowHeight = UITableViewAutomaticDim
我是一名优秀的程序员,十分优秀!