UITableView中的多个UICollectionView在滚动中交织在一起

问题描述 投票:0回答:1

我对iOS开发完全不熟悉,所以我的问题可能是一个noob问题,但我不知道如何让我的UICollectionViews滚动彼此分开。当我滚动我的顶部UICollectionView,向下滚动,我的其他UICollectionView也滚动而不触及它。

UICollectionView配置为水平滚动。我想要实现的就像Netflix所做的那样。用户可以水平滚动,而项目列表则水平显示。到目前为止一切正常,除了滚动一个列表使其他滚动重绘时(我猜,因为我只能看到它发生在设备上尚未显示的列表上。)

我希望我已经正确地描述了我的问题,你是否可能缺少一些信息来帮助我解决这个问题,我当然乐意提供。

所以我的UITableController看起来像这样:

class MoviesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var containerView: UIView!
@IBOutlet weak var tableView: UITableView!

let categories = ["In theaters", "Popular", "Upcoming", "Top rated"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    containerView.backgroundColor = UIColor.init(hex: "232637")
    tableView.backgroundColor = UIColor.init(hex: "232637")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSections(in tableView: UITableView) -> Int {
    return categories.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    tableView.rowHeight = 332;
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! TableViewCell
    return cell
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 42;
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return categories[section]
}

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
    view.tintColor = UIColor.init(hex: "232637")
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.textColor = UIColor.init(hex: "ff5959")
}

}

我的UITableViewCell看起来像这样:

class TableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var collectionView: UICollectionView!

let imageNames = //Some filler data
let gameNames  = //Some filler data

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    collectionView.backgroundColor = UIColor.init(hex: "232637")
    collectionView.contentInset = UIEdgeInsets(top: 0,left: 0,bottom: 32,right: 0)
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageNames.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! MovieCell
    //cell.imageView.imageFromURL(urlString: imageNames[indexPath.row])

    let image = UIImageView(image: UIImage(named: "img-logo"))
    image.contentMode = .scaleAspectFit
    image.frame = cell.containerView.bounds
    image.layer.cornerRadius = 10;
    image.clipsToBounds = true;
    image.imageFromURL(urlString: imageNames[indexPath.row])
    cell.containerView.addSubview(image)
    cell.containerView.backgroundColor = UIColor.init(hex: "232637")

    return cell
}

}

而我的UICollectionViewCell看起来像这样:

class MovieCell: UICollectionViewCell {

@IBOutlet weak var containerView: UIView!

}

我的观点看起来像这样:

My views looks like this

ios swift uitableview uicollectionview nested-lists
1个回答
2
投票

听起来你没有考虑到重复使用单元格(表格视图单元格和集合视图单元格)这一事实。因此,如果表视图包含集合视图并且您滚动该集合视图然后滚动表视图,则表视图单元格将在新行中重用,并且其中的先前滚动的集合视图仍保持滚动到您之前放置的位置。如果这不是您想要的,那么当您发现正在重用该单元时,由您来重置集合视图的滚动位置。

什么给了我一个线索,你可能不理解细胞再利用是这一行:

 cell.containerView.addSubview(image)

这是错误的,因为即使重复使用单元格,你也会这样做,这意味着你的某些单元格最终将会有数十个图像视图相互叠加,从而减慢速度并最终导致内存不足。这不是你问的问题,但它表明你不了解细胞再利用的含义。

© www.soinside.com 2019 - 2024. All rights reserved.