如何在 Swift 中计算表格视图中单元格的高度?

问题描述 投票:0回答:0

我想使表格视图中的特定单元格变得粘性,为此,我创建了一个标题,当该单元格即将消失并保持在顶部时会出现该标题,如下所示

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
    let offsetY = scrollView.contentOffset.y



    
    if offsetY >= rectOfInfoCouponsAvailable  {
        if stickyHeaderView == nil {
            stickyHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 100))
            view.addSubview(stickyHeaderView!)
            configureStickyHeaderView()
        }
    } else {
       
        stickyHeaderView?.removeFromSuperview()
        stickyHeaderView = nil
    }
}

我目前正在为 rectOfInfoCouponsAvailable 分配一个常量值,但它并不适用于所有设备。我希望这个值是我想要的特定单元格变得可见的高度。如何根据设备或表格视图的内容动态计算此高度?

我尝试使用以下代码,但它无法正常工作,因为它总是返回 0:

let indexPath = IndexPath(row: 2, section: 0)  
if let rectForCell = tableView.rectForRow(at: indexPath) {
    let rectOfInfoCouponsAvailable = rectForCell.size.height
   
}

如有任何帮助或建议,我们将不胜感激。预先感谢您的帮助!

ios swift uitableview cell
© www.soinside.com 2019 - 2024. All rights reserved.