目前,我已经将UICollectionView嵌入到UItableViewCell中,到目前为止,除我尝试使用横向模式外,其他一切都按预期工作。它包含相同的高度。作为“人像”模式,问题是我需要它具有更快的响应速度。
这是我的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
时会发生这种情况答案 0 :(得分:0)
您可以使用。只需检查设备方向,然后执行大小调整代码,然后重新加载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)")
}