Сделайте UITableViewCell подходящим для UICollectionView без tableView heightForRowAt - PullRequest
0 голосов
/ 02 ноября 2018

В настоящее время я встроил UICollectionView в UItableViewCell, поэтому пока все работает, как и ожидалось, за исключением случаев, когда я пытаюсь сделать ландшафтный режим. Он содержит одинаковую высоту. в портретном режиме, и проблема в том, что мне нужно, чтобы он был более отзывчивым.

Вот мои настройки TableView heightForRow при:

    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

enter image description here

1 Ответ

0 голосов
/ 02 ноября 2018

вы можете использовать. Просто проверьте ориентацию устройства, введите код изменения размера и перезагрузите 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)")        
}
...