首页 > 解决方案 > 在集合视图中找到中间单元格并更改其内容

问题描述

我的目标是在中间单元格中有全文,在其他单元格中截断文本,但是随着中间单元格的变化,这种样式不应该改变(意味着中间的一个全文和其他的被截断)。

预设:在水平滚动的集合视图中无限滚动,单元格中有标签。

我有 3 个单元格,它们应该看起来像这样 [ 1D ]---[ 1 Day ]---[ 1D ]

标签: iosswiftuicollectionviewindexpath

解决方案


您可以执行以下操作来找到中间单元格:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell:UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CELL_ID", for: indexPath)

    let center = self.view.convert(collectionView.center, to: collectionView)
    let index = collectionView.indexPathForItem(at: center)

    if (indexPath == index) {
        // truncated text
    } else {
        // don't truncated text
    }

    return cell
}

推荐阅读