美文网首页
swift-cell高度自适应estimatedRowHeigh

swift-cell高度自适应estimatedRowHeigh

作者: 授之以渔不如授之以鱼 | 来源:发表于2021-02-27 14:53 被阅读0次
    lazy var tableView: UITableView = {[unowned self] in
        let new = UITableView(frame: CGRect.zero, style: .grouped)
        new.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
        new.delegate = self
        new.dataSource = self
        new.showsVerticalScrollIndicator = false
        new.showsHorizontalScrollIndicator = false
        new.contentInset = UIEdgeInsets.zero
        new.tableHeaderView = self.headerView
        new.estimatedRowHeight = 80//不设置也行
        new.rowHeight = UITableViewAutomaticDimension//不设置也行
        return new
    }()

heightForRowAt给高度为UITableViewAutomaticDimension

    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let form = self.vm!.grounds[indexPath.section].items[indexPath.row]
        switch form.type as! ResumCellForm {
        case .pdesc:
            return 120
        case .video:
            return 80
        case .accout:
            return 80
        case .photo:
            return UITableViewAutomaticDimension
        case .zone:
            return 100
        case .auth:
            return UITableViewAutomaticDimension
        case .detailInfo:
            return UITableViewAutomaticDimension
        case .spouse:
            return UITableViewAutomaticDimension
        case .gift:
            return 100
        default:
            return 80
        }
    }

最后cell里面要给top和bottom的距离equalToSuperview().offset(16)这样写,不然会冲突

  override func ai_setupLayout() {
        super.ai_setupLayout()
        
        infoDetailView.snp.makeConstraints { (make) in
            make.left.equalTo(80)
            make.right.equalTo(-40)
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview().offset(-16)
            make.height.equalTo(CGFloat.leastNormalMagnitude)
        }
        
    }
    
    func updateLayout(){
        let infoHeight = 5 * 20.0
        infoDetailView.snp.updateConstraints { (make) in
            make.height.equalTo(infoHeight)
        }
    }

相关文章

网友评论

      本文标题:swift-cell高度自适应estimatedRowHeigh

      本文链接:https://www.haomeiwen.com/subject/tvcifltx.html