2017-10-18 43 views
0

我有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] = [] 

    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

回答

0

從你的描述,這似乎是再利用的問題。換句話說,在回滾到頂部之後,位於索引1的單元格用來自第四位置的對象裝飾。
查看您的數據源,它可能會在滾動時修改或者cellForRowAtIndexPath:未正確裝飾單元格。

+0

我在考慮重用。但我不明白如何處理它。你可以幫我嗎?如果需要我的代碼,我會在問題 –

+0

添加代碼可能會幫助,我會看看它的明天。 – Iosif

+0

我加了viewController和tableViewCell,我希望這就夠了 –

0

我猜問題是你的單元格在var theHalls被設置時不會重新加載它的collectionView。

編輯:

您可以修改你這樣的代碼:

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

最有可能是的,但我不明白如何解決它 –

+0

沒有幫助我:(顯然我有一個更嚴重的問題 –

0

我被這行代碼的方法cellForRow在添加的tableView幫助

cell.collectionView.contentOffset = .zero