gpt4 book ai didi

ios - index ScrollTo 不适用于 TableViewCell Swift 中的最后一个 CollectionView

转载 作者:行者123 更新时间:2023-11-30 12:15:37 26 4
gpt4 key购买 nike

我在 TableView 中创建了一个 CollectionView,用于垂直和水平滚动以及可自定义单元格。到目前为止这有效。

我希望它在第一次打开应用程序时(如果我手动刷新页面)在特定 IndexPath 处显示来自 CollectionView 的特定单元格。我得到了正确的 IndexPath,它适用于每个 TableViewCell(其中 CollectionView 位于内部),除了最后一个。

这是我的代码:

import UIKit
import RealmSwift


class HomeVTwoTableViewCellSmall: UITableViewCell{

//belongs to the center Method
var onceOnly = false
var counterIndexPath = 0

//serves as a translator from ChannelName to the ChannelId
var channelOverview: [String:String] = ["Some: Values"]

//Initiaize the CellChannel Container and Live Programm
var cellChannel: Results<Community>!
var liveProgramm: Results<Live>?

//Initialize the translated ChannelId
var channelId: String = ""
override func prepareForReuse() {
channelId = ""
}


var channelTitle = "" {
didSet {
let realm = try! Realm()
//Getting the ChannelId from Dictionary
self.channelId = self.channelOverview[self.channelTitle]!

//load data from Realm into variables
self.cellChannel = realm.objects(Community.self).filter("channelId = \(self.channelId) ").sorted(byKeyPath: "startTime")
self.liveProgramm = realm.objects(Live.self).filter("channelId = \(self.channelId) ")
self.collectionView.reloadData()
}
}
@IBOutlet weak var collectionView: UICollectionView!

override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
}
}

extension HomeVTwoTableViewCellSmall: UICollectionViewDataSource,UICollectionViewDelegate {

//MARK: Datasource Methods
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return (cellChannel.count)
}

//gets the IndexPath to use
func getLiveIndexPath(){
var counter = 0
counterIndexPath = 0
for i in 0 ..< cellChannel.count
{
if (cellChannel[i].communityId != liveProgramm?[0].communityId) {
counter += 1
}
else {
counterIndexPath = counter
return
}
}
}

//TODO: Center collection view on Live programm
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {

getLiveIndexPath()

if !onceOnly {
let indexToScrollTo = IndexPath(item: counterIndexPath, section: 0)
//Change position of the Live cell by Changing position "at: ..."
self.collectionView.scrollToItem(at: indexToScrollTo, at: .centeredHorizontally, animated: false)
onceOnly = true
}
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCellSmall", for: indexPath) as? HomeVTwoCollectionViewCellSmall else
{
fatalError("Cell has wrong type")
}
let realm = try! Realm()

let selectedCommunityObject = realm.object(ofType: Community.self, forPrimaryKey: cellChannel[indexPath.row].communityId)!

//removes the old image and Titel
cell.imageView.image = UIImage(named: "No Image")
cell.titleLbl.text = nil

//inserting the channel specific data
let url : String = (cellChannel[indexPath.row].pictureId)
let name : String = (cellChannel[indexPath.row].communityName)
cell.titleLbl.text = name
cell.imageView.downloadedFrom(link :"")
return cell
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedCommunity = (cellChannel[indexPath.row].communityId)
let home = HomeViewController()
home.showCommunityDetail()
}
}

似乎在最后一个CollectionView中,“onceOnly” bool 值已经是true,但我不明白为什么它在开始时有效,但在最后一个时不起作用。它滚动但不在正确的 IndexPath

最佳答案

很可能,您遇到了重用 UITableViewCell 的问题。发生的情况是,您最初将 channel A 的一个新单元出队,并且它正在正确设置和显示,包括将 onceOnly 设置为 true。然后,此单元格被 tableview 重用,并出队以用于 channel B。但在这种情况下,onceOnlycounterIndexPath 仍设置有该信息对于A。

您已经在实现prepareForReuse。您只需在那里重置其他信息即可获得您需要的信息。

override func prepareForReuse() {
channelId = ""
onceOnly = false
counterIndexPath = 0
}

关于ios - index ScrollTo 不适用于 TableViewCell Swift 中的最后一个 CollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45463975/

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