在集合视图中查找中间单元格并更改其内容

时间:2018-06-13 08:13:28

标签: ios swift uicollectionview indexpath

我的目标是在中间单元格中使用完整文本,在其他单元格中使用截断文本,但是如果更改中间单元格,则此样式不应更改(表示中间一个全文而其他单词被截断)。

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

我有3个细胞,他们应该看起来像这样[1D] --- [1天] --- [1D]

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情来找到中间单元格:

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
}
相关问题