gpt4 book ai didi

ios - 页面加载时滚动后如何访问 UICollectionView 中的特定单元格?

转载 作者:行者123 更新时间:2023-11-28 14:15:33 26 4
gpt4 key购买 nike

我正在尝试建立这样的时间表:

enter image description here

时间轴中的每个单元格代表一个月,因此 May 是水平 collectionView 中的最后一个单元格。我想要的是当 collectionView 加载时,如果它还不可见,它会自动滚动到可能,然后调用单元格上的特定函数来绘制箭头。下面是我用来尝试执行此操作的代码。

    override func viewDidLoad() {
super.viewDidLoad()
self.timeline.register(UINib(nibName: "TimelineMonthViewCell", bundle: nil), forCellWithReuseIdentifier: "TimelineMonthViewCell")
self.collectionView.register(UINib(nibName: "PostCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "PostCollectionViewCell")
pageControl.hidesForSinglePage = true
self.navigationItem.largeTitleDisplayMode = .never

UserService.posts(for: User.current,tagString: self.selectedTagTitles.first!) { (postStuff) in

self.posts = postStuff.0
self.postIds = postStuff.1
self.timeline.dataSource = self
self.collectionView.dataSource = self
self.timeline.delegate = self
self.collectionView.delegate = self
self.firstPostDate = self.posts.first?.timeStamp
self.currentPostDate = self.posts.last?.timeStamp
}
}

override open func viewDidAppear(_ animated: Bool) {

super.viewDidAppear(animated)

let indexToScrollTo = IndexPath(row: self.posts.count - 1, section: 0)
self.collectionView.scrollToItem(at: indexToScrollTo, at: .left, animated: false)
let firstPostDate = self.posts.first?.timeStamp
let diff = self.posts.last?.timeStamp.months(from: (firstPostDate?.startOfMonth())!)
//self.currentPostMonth = diff
let monthCellIndexPath = IndexPath(row: diff!, section: 0)
self.timeline.scrollToItem(at: monthCellIndexPath, at: .centeredHorizontally, animated: false)
let months = self.posts.last?.timeStamp.months(from: (self.posts.first?.timeStamp.startOfMonth())!)
print("cells are",self.timeline.indexPathsForVisibleItems)
print(months,"cell months")
if let optionalCell = self.timeline.cellForItem(at: IndexPath(row: months!, section: 0)) {
let cell = optionalCell as! TimelineMonthViewCell
let day = Calendar.current.component(.day, from: self.currentPostDate!)
cell.drawArrow(day: day)
} else {
print("cell out of range")
}
}

这几乎可以工作,除了两个问题。

首先,初始单元格会在自动滚动到最后一个单元格之前闪烁。我认为这是因为我使用的是 viewDidAppear 而不是 viewWillAppear。但是,当我使用 viewWillLoad 时,应用程序崩溃并出现错误:Thread 1: EXC_BAD_ACCESS (code=1, address=0xfffffffffffffff8)

其次,打印出的self.timeline.indexPathsForVisibleItems为:[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4]]。所以它查看前 5 个而不是它刚刚滚动到的最后 5 个。然后它找不到单元格:“单元格超出范围”,因为索引约为 28(最后一个单元格)。

那么我怎样才能让它自动滚动到最后一个单元格并正确找到它呢?

最佳答案

不要在当前位置设置这些行

self.timeline.dataSource = self
self.collectionView.dataSource = self
self.timeline.delegate = self
self.collectionView.delegate = self

viewDidLoad 之上可能会更好,以利用加载 vc 时的第一次自动重新加载,也在它们的位置插入

self.timeline.reloadData()
self.collectionView.reloadData()

你可以在里面插入滚动码

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {  } 

或在 ViewDidAppear 中,如果您当前的 for 循环不是异步的

关于ios - 页面加载时滚动后如何访问 UICollectionView 中的特定单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52211700/

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