如何在willDisplay单元格回调中访问UICollectionView中的特定单元格

时间:2018-09-05 18:17:37

标签: ios swift uiscrollview uicollectionview

编辑:大图:我正在尝试像这样构建时间轴:enter image description here时间轴中的每个单元格代表一个月,因此May是水平collectionView中的最后一个单元格。我想要的是在collectionView加载时自动滚动到可能不可见的位置,然后在单元格上调用特定函数以绘制箭头。我用来尝试执行此操作的代码如下。

这是我的代码:

var onceOnlyCollectionView = false
var onceOnlyTimeline = false
internal func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    if collectionView == self.collectionView {
        if !onceOnlyCollectionView {
            let indexToScrollTo = IndexPath(row: self.posts.count - 1, section: 0)
            collectionView.scrollToItem(at: indexToScrollTo, at: .left, animated: false)
            onceOnlyCollectionView = true
        }
    } else if collectionView == self.timeline {
        if !onceOnlyTimeline {
            let firstPost = posts.first?.timeStamp
            let firstOfFirstMonth = firstPost?.startOfMonth()
            let diff = posts.last?.timeStamp.months(from: firstOfFirstMonth!)
            //self.currentPostMonth = diff
            let monthCellIndexPath = IndexPath(row: diff!, section: 0)
            timeline.scrollToItem(at: monthCellIndexPath, at: .centeredHorizontally, animated: false)
            let months = self.posts.first?.timeStamp.startOfMonth().months(from: (self.posts.last?.timeStamp)!)
            print("cells are",self.timeline.visibleCells)
            if let optionalCell = 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")
            }
            onceOnlyTimeline = true
        }
    }
}

现在,它确实成功滚动到了May,但是它打印“单元格为[]”和“单元格超出范围”,并且没有绘制红色箭头。这是怎么回事,我如何访问这个新滚动的单元?

1 个答案:

答案 0 :(得分:0)

您可以像这样简单地访问“收藏夹视图”的特定单元格

create unique index on the_table (coalesce(a,-1), coalesce(b, -1), coalesce(c, -1));