带约束的嵌套集合视图的意外行为 (Swift 4)

2024-05-16

我的表格视图中有一个单元格,其中包含水平分页集合视图。

该集合视图的每个页面内都有一个垂直集合视图。

为了避免“滚动滚动”问题,我在垂直集合视图中禁用了垂直滚动。

垂直集合视图的单元格计数不是静态的,可以是任意数字。

因此,这会产生一个问题:集合视图(您必须向我询问是哪一个视图)将不再适合表视图的单元格。

为了解决这个问题,我通过更改故事板中的约束来更改表视图的单元格高度......但问题仍然存在。

这是帮助说明问题的图像:

这是我的代码:

class myTBViewController: UITableViewController , UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView.tag == 10 {
    return 2
    } else {
        return 30
    }
}


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

        cell.backgroundColor = UIColor.yellow
        cell.listCV.clipsToBounds = true
    return cell
    } else {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "showAdvCell", for: indexPath) as! showAdvCell

        cell.backgroundColor = UIColor.red

        return cell

    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

    if collectionView.tag == 10 {
    let collectionViewHeight = collectionView.frame.height
    let itemsHeight        = collectionView.contentSize.height

    let topInset = ( collectionViewHeight - itemsHeight ) / 4

    return UIEdgeInsetsMake(topInset ,0, 0 , 0)

    } else {

        let collectionViewHeight = collectionView.frame.height
        let itemsHeight        =  CGFloat(80)

        let topInset = ( collectionViewHeight - itemsHeight ) / 4

        return UIEdgeInsetsMake(topInset ,0, 0 , 0)


    }
}

var headerSegment = UISegmentedControl()

override func viewDidLoad() {
    super.viewDidLoad()

    headerSegment.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 30)
    let items = ["فروشگاه ها", "دیوار"]
    headerSegment = UISegmentedControl(items: items)
    headerSegment.selectedSegmentIndex = 0
    headerSegment.addTarget(self, action: #selector(selectPage), for: UIControlEvents.valueChanged)
    let frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200)
    let headerImageView = UIImageView(frame: frame)
    let image: UIImage = UIImage(named: "1")!
    headerImageView.image = image

    let frame3 = CGRect(x: 0, y: 0, width: 80, height: 80)
    let headerImageView2 = UIImageView(frame: frame3)
    let image2: UIImage = UIImage(named: "1")!
    headerImageView2.image = image2
    headerSegment.addSubview(headerImageView2)
    headerSegment.bringSubview(toFront: headerImageView2)
    headerImageView2.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: 200)
    headerSegment.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width + 10 , height: 40)
    headerSegment.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: 200)
    let mainView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 230))
    let headerSegmentView = UIView(frame : CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 40))
    headerSegmentView.center = headerSegment.center
    headerSegmentView.backgroundColor = UIColor.white
    mainView.addSubview(headerImageView)
    mainView.bringSubview(toFront: headerImageView)
    mainView.addSubview(headerSegmentView)
    mainView.bringSubview(toFront: headerSegmentView)
    mainView.addSubview(headerSegment)
    mainView.bringSubview(toFront: headerSegment)
    mainView.addSubview(headerImageView2)
    mainView.bringSubview(toFront: headerImageView2)
    self.tableView.tableHeaderView = mainView

    headerImageView2.layer.borderWidth = 2
    headerImageView2.layer.masksToBounds = false
    headerImageView2.layer.borderColor = UIColor.init(white: 255/255, alpha: 0.3).cgColor
    headerImageView2.layer.cornerRadius = headerImageView2.frame.height/2
    headerImageView2.clipsToBounds = true

}

@objc func selectPage() {

    let cell = self.tableView.dequeueReusableCell(withIdentifier: "myTBViewCell") as! myTBViewCell

    switch headerSegment.selectedSegmentIndex {
    case 0:
        print("0")
        cell.myTBCollectionView.scrollToItem(at:IndexPath(item: 0, section: 0), at: .top, animated: false)
    case 1:
        print("1")
        cell.myTBCollectionView.scrollToItem(at:IndexPath(item: 1, section: 0), at: .top, animated: false)
    default:
        print("0")
        cell.myTBCollectionView.scrollToItem(at:IndexPath(item: 0, section: 0), at: .top, animated: false)

    }

}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 1
}


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

    tableView.estimatedRowHeight = 200
    tableView.rowHeight = UITableViewAutomaticDimension

    return 600
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCell(withIdentifier: "myTBViewCell", for: indexPath) as! myTBViewCell

    return cell
}


}

我有一个类似的问题,最后,我通过创建 CollectionView 的高度约束解决了它。

比根据 CollectionView ContentSize 设置 CollectionView 高度约束。

self.collectionViewHeight.constant = self.recentCollectionView.collectionViewLayout.collectionViewContentSize.height

另一种方法是重写 tableViewCell 的 systemLayoutSizeFitting 方法

ex:

override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)
        self.tagCollectionView.layoutIfNeeded()
        return self.tagCollectionView.collectionViewLayout.collectionViewContentSize
    }

另外,您不需要在表视图委托 heightForRowAtIndexPath 中返回固定的 600 高度

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带约束的嵌套集合视图的意外行为 (Swift 4) 的相关文章

随机推荐