目前,我已将 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 时会发生这种情况
最佳答案
你可以使用。只需检查设备方向并执行调整大小代码并重新加载 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/