簡體   English   中英

在行內滾動項目的一些問題

[英]Some problems with scrolling Item inside Rows

我在tableView里面有collectionView collectionView需要水平滾動圖像, tableView需要垂直滾動帖子。 當我有3 行時,我沒有問題,但是當我創建4 行時,我在行內滾動項目時遇到了問題。 如果我開始在4 行上滾動,則在 1 重復滾動,如果我開始在1 行上滾動,則在 4 重復滾動。

可能是什么問題以及如何解決? 也許

可以檢查.gif文件。 我從名稱“ Oko ”的第一開始,如果我向下滾動至第四並向右滾動collectionCell返回到第一行,我會看到下一個圖像名稱“ City”,但是必須有名稱“ Oko

我的代碼:

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圖像

從您的描述來看,這似乎是重用的問題。 換句話說,從上到下滾動到頂部后,索引1處的單元格將用對象裝飾。
看一下您的數據源,它可能在滾動時被修改,或者cellForRowAtIndexPath:沒有正確裝飾單元格。

我猜問題是當設置var theHalls時,您的單元格不會重新加載其collectionView。

編輯:

您可以這樣修改代碼:

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

cellForRow方法的tableView中添加的這一行代碼為我提供了幫助

cell.collectionView.contentOffset = .zero

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM