gpt4 book ai didi

ios - 滚动时重复使用一行内的项目

转载 作者:搜寻专家 更新时间:2023-11-01 06:31:24 24 4
gpt4 key购买 nike

我在 tableView 中有 collectionViewcollectionView 需要水平滚动图片,tableView 用于垂直滚动帖子。当我有 3 个时,我没有问题,但是当我创建 4 个时,我在行内滚动项目时遇到问题。如果我开始在第 4 上滚动,则在 1 上重复滚动,如果我在第 1 上开始滚动,则重复滚动 4.

可能是什么问题以及如何解决?可能是

可以检查.gif 文件。我从名称“Oko”的第 1 开始,如果我向下滚动到第 4 并向右滚动collectionCellreturn 在 1 row 我看到下一个图像名称“City”,但必须有名称“Oko

我的代码:

View Controller :

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

}

表格 View 单元格:

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

最佳答案

您在单元格中重复使用 Collection View ,这是正确的,但这意味着当重复使用单元格时,contentOffset 也会从之前滚动到的内容中保留下来。当您通过执行以下操作设置您的单元格时,只需重置 cellForRowAtIndexPath 中的 contentOffset 就足够了:

cell.collectionView.contentOffset = .zero

值得一提的是,我确实看到您的单元格中有一个名为 lastContentOffset 的属性,该属性尚未执行任何操作,我怀疑您将尝试使用它来保留偏移量对于给定的单元格,当它滚出 View 时,您可以在它返回 View 时再次设置它(而不是总是重置)。

如果您打算这样做,那么在单元格中拥有该属性将不起作用。您需要在包含的 View Controller 中与数据模型一起存储每个单元格的偏移量列表。然后,您可以将给定单元格的偏移量保存在 didEndDisplayingCell 中,并将其设置在 cellForRowAtIndexPath 中,而不是像我上面那样设置在 .zero 中。

关于ios - 滚动时重复使用一行内的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46833406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com