2017-10-19 62 views
1

我有collectionView内部tableViewcollectionView需要水平滚动图像,tableView垂直滚动的帖子。当我有3 我没有问题,但是当我创建4 我有滚动内部行的问题。如果我开始滚动的行,滚动重复 1,同样的事情,如果我开始滚动1个滚动重复上 4.滚动时重复使用内部项目

可能是什么问题,如何解决它?可能是

可以检查.gif文件。我开始 1个行的名称为“尾子”如果我在行向下滚动和向右滚动collectionCell1个我看到未来的图像名称“城市”回归,但有必须命名为 “尾子

我的代码:

的ViewController:

class PhotoStudiosViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating { 

    @IBOutlet weak var tableView: UITableView! 

    var theStudios: [Studio] = [] 
    var filteredStudios: [Studio] = [] 

    var studiosRef: DatabaseReference! 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(true) 

     tableView.estimatedRowHeight = 475 
     tableView.rowHeight = UITableViewAutomaticDimension 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     studiosRef = Database.database().reference(withPath: "PhotoStudios1") 

     studiosRef.observe(.value, with: { (snapshot) in 

      for imageSnap in snapshot.children { 

       let studioObj = Studio(snapshot: imageSnap as! DataSnapshot) 

       self.theStudios.append(studioObj) 

      } 

      self.tableView.reloadData() 

     }) 

    } 
    // MARK: - TableView 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     if searchController.isActive && searchController.searchBar.text != "" { 

      return filteredStudios.count 

     } 

     return theStudios.count 

    } 

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

     cell.currentPageNumber.text = "1/\(theStudios[indexPath.row].halls.count)" 

     if searchController.isActive && searchController.searchBar.text != nil { 
      cell.theHalls = filteredStudios[indexPath.row].halls 
     } else { 
      cell.theHalls = theStudios[indexPath.row].halls 
     } 

     cell.nameLabel.text = theStudios[indexPath.row].studioName 

     cell.addressLabel.text = theStudios[indexPath.row].studioAddress 

     cell.logoLabel.sd_setImage(with: URL(string: theStudios[indexPath.row].studioLogo)) 

     cell.didSelectAction = { 

      (innerPath) in 

      self.showDetailsView(indexPath, cellPath: innerPath) 

     } 

     return cell 

    }  

TableViewCell:

class PhotoStudiosTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate, UICollectionViewDelegateFlowLayout { 

    @IBOutlet weak var button: UIButton! 
    @IBOutlet weak var logoLabel: UIImageView! 
    @IBOutlet weak var nameLabel: UILabel! 
    @IBOutlet weak var addressLabel: UILabel! 
    @IBOutlet weak var collectionView: UICollectionView! 
    @IBOutlet weak var currentPageNumber: UILabel! 

    var didSelectAction: ((IndexPath) ->())? 

    var theHalls: [Hall] = [] { 
     didSet { 
      collectionView.reloadData() 
     } 
    } 

    var lastContentOffset = CGPoint.zero 

    override func prepareForReuse() { 
     super.prepareForReuse() 

     resetCollectionView() 

    } 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     currentPageNumber.layer.zPosition = 2 
     currentPageNumber.layer.cornerRadius = 15.0 
     currentPageNumber.clipsToBounds = true  

    } 

    func resetCollectionView() { 
     guard !theHalls.isEmpty else { return } 
     theHalls = [] 
     collectionView.reloadData() 
    } 

    // MARK: - CollectionView 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     return theHalls.count 

    } 

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

     cell.hallName.text = theHalls[indexPath.item].hallName 

     cell.priceLabel.text = theHalls[indexPath.item].hallPrice 

     cell.metrslabel.text = theHalls[indexPath.item].hallMetrs 

     cell.photoStudioImage.sd_setImage(with: URL(string: theHalls[indexPath.item].hallImage)) 

     return cell 

    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

     didSelectAction?(indexPath) 

    } 

} 

git image

回答

3

你重用收集意见,你的细胞,这是正确的,但是这意味着contentOffset还剩下无论从任何已滚动到前面当一个单元被重用时。它应该是足够的,只是重置contentOffset在cellForRowAtIndexPath当你正在做类似设置你的细胞:

cell.collectionView.contentOffset = .zero 

有一两件事值得一提的是,我看到你有一个名为在你的细胞没有按lastContentOffset财产还没有做任何事情,我怀疑你会试图用它来保持给定单元格的偏移量,当它滚动出视图时,以便当它重新进入视图(而不是始终重置)时再次设置它。

如果你打算这样做,让单元格中的属性不起作用。您需要为包含在视图控制器中的数据模型旁边存储的每个单元格设置一个偏移量列表。然后,您可以将给定单元格的偏移量保存在didEndDisplayingCell中,并将其设置为cellForRowAtIndexPath而不是.zero,如上所述。

+0

它帮我'cell.collectionView.contentOffset = .zero'。非常感谢,第二天试图解决这个问题。关于其他我会认为我需要它。 – onpro

相关问题