【问题标题】:Make UITableViewCell fit UICollectionView without tableView heightForRowAt使 UITableViewCell 适合 UICollectionView 而没有 tableView heightForRowAt
【发布时间】:2018-11-02 03:18:28
【问题描述】:

目前我已经在 UItableViewCell 中嵌入了一个 UICollectionView 到目前为止一切都按预期工作,除非我尝试使用横向模式。它包含相同的高度。作为纵向模式,问题是我需要它来响应更快。

这是我的 TableView heightForRowAt 设置:

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 0 {
        return 360
    } else {
        return 230
    }
}

在作为 UICollectionViewCell 的自定义 UITableViewCell 中,我设置了这样的约束。

class MenuTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

var timerTest : Timer?
weak var myParent:UITableViewController?

let menuOptions = [AppMenu(id:"1", name:"Buscar ", imageUrl:"search"),
                   AppMenu(id:"2", name:"Fichas ", imageUrl:"datasheet"),
                   AppMenu(id:"3", name:"Tabla ", imageUrl:"table"),
                   AppMenu(id:"4", name:"Preguntas ", imageUrl:"faq")]

var myCollectionView: UICollectionView = {

    let layout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 5, right: 20)
    layout.scrollDirection = .vertical

    let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
    view.backgroundColor = UIColor(red: 0.224, green: 0.698, blue: 0.667, alpha: 1.0)
    view.showsHorizontalScrollIndicator = true
    return view
}()


override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    addSubview(myCollectionView)

    myCollectionView.delegate = self
    myCollectionView.dataSource = self
    myCollectionView.register(MenuCollectionViewCell.self, forCellWithReuseIdentifier: "collectionCellId")
    myCollectionView.translatesAutoresizingMaskIntoConstraints = false
    myCollectionView.isScrollEnabled = false
    myCollectionView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
    myCollectionView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    myCollectionView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    myCollectionView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return menuOptions.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCellId", for: indexPath) as! MenuCollectionViewCell

    print(indexPath.row)

    let menu = menuOptions[indexPath.row]
    cell.nameLabel.text = menu.name
    cell.imageView.image = UIImage(named: menu.imageUrl)
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    var cellWidth = CGFloat()
    cellWidth = (CGFloat((myCollectionView.frame.size.width / 2) - 30) > 157.5) ? 157.5 : CGFloat((myCollectionView.frame.size.width / 2) - 30)

    var cellHeight = CGFloat()
    cellHeight = (cellWidth * 180 / 180) > 157.5 ? 157.5 :  (cellWidth * 180 / 180)

    return CGSize(width: cellWidth, height: cellHeight)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let menu = menuOptions[indexPath.row]
    print(menu.name)

    let searchCarViewController = VehicleCategoryTableController()
    myParent?.navigationController?.pushViewController(searchCarViewController, animated: true)
}
}

当我删除 heightForRowAt

时会发生这种情况

【问题讨论】:

  • 你应该使用尺寸类(具有紧凑的高度和任何宽度)来支持横向,然后你可以使用 viewcontroller 的 traitCollection 属性检查当前的尺寸类
  • 您已经在使用collectionViewLayout sizeForItemAt indexPath 来设置CollectionViewCell 的高度。那你为什么要使用heightForRowAt indexPath。您可以使用UITableView.automaticDimension 并设置适当的约束。
  • @Amit 我删除了 heightForRowAt 并且单元格缩小了,你可以看到 myCollectionView 对上、下、左、右有约束
  • 检查这个:stackoverflow.com/questions/48986188/…。希望对您有所帮助。
  • @Amit 先生,我不使用 Storyboard。 :(

标签: ios swift uitableview heightforrowatindexpath


【解决方案1】:

你可以使用。只需检查设备方向并执行调整大小代码并重新加载 TableView。

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
    var text=""
    switch UIDevice.current.orientation{
    case .portrait:
        text="Portrait"
    case .portraitUpsideDown:
        text="PortraitUpsideDown"
    case .landscapeLeft:
        text="LandscapeLeft"
    case .landscapeRight:
        text="LandscapeRight"
    default:
        text="Another"
    }
    NSLog("You have moved: \(text)")        
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 2014-08-21
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多