2017-10-19 64 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

相關問題