目前,我已经将UICollectionView嵌入到UItableViewCell中,到目前为止,除我尝试执行横向模式外,其他所有工作都按预期进行。它包含相同的高度。作为人像模式,问题是我需要它具有更快的响应速度。

这是我的TableView heightForRowAt设置:

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

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


在我的自定义UITableViewCell(它是一个UICollectionViewCell)中,我像这样设置约束。

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时会发生这种情况

ios - 使UITableViewCell适合UICollectionView而没有tableView heightForRowAt-LMLPHP

最佳答案

您可以使用。只需检查设备方向并执行大小调整代码,然后重新加载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)")
}

关于ios - 使UITableViewCell适合UICollectionView而没有tableView heightForRowAt,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53112235/

10-08 20:51