我有一個UITableView,在UITableViewCell中,我添加了視圖,并試圖根據螢屏高度或UITableView的height來設定該視圖或單元格的高度。最初,我能夠做到這一點,但當在viewWillAppear重新加載表時,無法為該視圖或單元格的高度設定適當的高度。是高度問題還是其他問題,無法確定。希望得到任何幫助,謝謝你的回答。這個問題的標題可能會引起混淆,或者你發現了任何不相關的代碼,那么歡迎提出建議(編輯)。
代碼:
import UIKit
class PageViewController。UIViewController {
@IBOutlet weak var tblPager: UITableView!
let screenHeight = UIScreen.main.bound.size.height
let vColors: [UIColor] = [.red, .blue, .orange, .brown, .cyan, .darkGray, .green, .]
var pageSizeHeight: CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
pageSizeHeight = screenHeight - (getTopSafeArea() getBottomSafeArea() 54)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
delay(interval: 10.0) {
print("10秒后重新加載表")
self.tblPager.reloadData()
}
}
}
/MARK:- TableView Delegate。
extension PageViewController。UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return vColors.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView. dequeueReusableCell(withIdentifier: "PageCell", for: indexPath) as! PageCell
cell.backgroundColor = vColors[indexPath.row]。
cell.viewHeight.constant = pageSizeHeight
return cell。
}
}
class PageCell: UITableViewCell {
@IBOutlet weak var vwPage: UIView!
@IBOutlet 弱 var viewHeight: NSLayoutConstraint!
}
func getTopSafeArea() -> CGFloat {
let window = UIApplication.share.keyWindow
let topPadding = window?.safeAreaInsets.top ? ? 0
return topPadding
}
func getBottomSafeArea() -> CGFloat {
let window = UIApplication.share.keyWindow
let bottomPadding = window?.safeAreaInsets.bottom ? ? 0
return bottomPadding
}
func delay(interval: TimeInterval, closure: @escaping () -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() interval) {
關閉()
}
}
輸出:
uj5u.com熱心網友回復:
如果所有的單元格都有相同的高度,你為什么不在viewWillLayoutSubviews方法中設定rowHeight屬性?
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
tblPager.rowHeight = tblPager.frame.height.
}
感謝@Desdenova對我的糾正
通過在PageViewController的viewDidLoad中設定dataSource和delegate為self,并在viewDidLayoutSubviews中設定rowHeight屬性,tableView的行為符合預期。
override func viewDidLoad() {
super.viewDidLoad()
tblPager.register(UINib(nibName: "PageTableViewCell", bundle: nil),forCellReuseIdentifier。"PageCell")
tblPager.dataSource = self
tblPager.委托 = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tblPager.rowHeight = tblPager.frame.size.height
}
uj5u.com熱心網友回復:
將它連接到tableView的高度,你的表視圖有你需要的所有尺寸。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) {
/ ...
cell.viewHeight.constant = tableView.frame.size.heigh
/ ...單元格.viewHeight.constant
}
或者洗掉viewHeight約束并使用委托方法來設定行高
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.size.height.
UPD:
TableView使用estimatedRowHeight來計算其滾動偏移量。如果我們啟用了分頁功能并且沒有配置estimatedRowHeight,這在重新加載時確實會產生一個問題。
解決方案1:
實作estimatedHeightForRowAt委托
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.size.height.
解決方案2:
設定tableView.improvedRowHeight = 0并使用heightForRowAt委托代替viewHeight約束
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/311750.html
標籤:
上一篇:是否可以在UITableView上面分層滾動一個ScrollView?
下一篇:在.NetCore(.Net5)中使用代碼先行遷移將ASPNetUsers主鍵資料型別從nvarchar更新為bigint。



