我想在 TableView 单元格中显示垂直数据收集 View 。但是当第一次从 json 重新加载数据时, TableView 的高度不会自动更改。但是当tableview向上滚动时,tableview的高度会发生如下变化
从 Json 重新加载数据时出现第一张图像: enter image description here
表格 View 向上滚动时的图像 enter image description here
这是我的代码:
View Controller
import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, reloadTable { func reloadTableData() { self.tableView.reloadData() self.tableView.beginUpdates() self.tableView.endUpdates() } @IBOutlet var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: Cell.tableView.rawValue) tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 100 tableView.tableFooterView = UIView() tableView.register(UINib(nibName: "SecondTableViewCell", bundle: nil), forCellReuseIdentifier: "SecondTableViewCell") } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 2 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.row == 0{ let cell = tableView.dequeueReusableCell(withIdentifier: "SecondTableViewCell", for: indexPath) as! SecondTableViewCell cell.name.text = "first data" return cell }else{ let cell = tableView.dequeueReusableCell(withIdentifier: Cell.tableView.rawValue, for: indexPath) as! TableViewCell cell.setNeedsLayout() cell.layoutIfNeeded() return cell } } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.row == 0{ return 100 }else { return UITableView.automaticDimension } } }
这是我的表格 View 单元格:
TableViewCell
import UIKit import Alamofire import SwiftyJSON struct dataJSON { var name: String } protocol reloadTable { func reloadTableData() } class TableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{ var reload: reloadTable? var dataJson : [dataJSON] = [] @IBOutlet var collectionView: UICollectionView! override func awakeFromNib() { super.awakeFromNib() fetchData() collectionView.delegate = self collectionView.dataSource = self collectionView.isScrollEnabled = false collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: Cell.collView.rawValue) let collViewLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout collViewLayout.itemSize = UICollectionViewFlowLayout.automaticSize layoutIfNeeded() setNeedsLayout() } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dataJson.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.collView.rawValue, for: indexPath) as! CollectionViewCell cell.detail.text = dataJson[indexPath.row].name return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: collectionView.frame.size.width / 2 - 10, height: 300) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 5 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 5 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3) } func fetchData(){ Alamofire.request("https://jsonplaceholder.typicode.com/users", method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON{ (response) in switch response.result{ case .success(let value): print(value) let json = JSON(value) let name = json["name"].stringValue print("nameesss: \(name)") json.array?.forEach({ (item) in let data = item["name"].stringValue self.dataJson.append(dataJSON(name: data)) }) self.collectionView.reloadData() self.reload?.reloadTableData() case .failure(let error): print(error) } } } override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize { self.collectionView.frame = CGRect(x: 0, y: 0, width: targetSize.width, height: 600) self.collectionView.layoutIfNeeded() return self.collectionView.collectionViewLayout.collectionViewContentSize } }
最佳答案
尝试使用
DispatchQueue.main.async { [weak self] in
self?.tableView.reloadData()
}
关于json - 如何快速修复tableview自动高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57335564/