swift 如何获取CollectionView的第一个单元格?

rslzwgfq  于 2023-02-18  发布在  Swift
关注(0)|答案(1)|浏览(160)

我想在Vc收到通知时改变第一个单元格的背景色。我使用了像blow这样的代码来实现这一点,但它总是得到nil:

@objc func getNotification() {
        let indexPath = IndexPath(row: 0, section: 0)
        if let cell = collectionView.cellForItem(at: indexPath) as? TestCell {
            cell.changeBackgroundColor()
        }
 }

如果我打印visibelCells,我得到了6个不同的单元格对象!(它只在屏幕上显示3个,并且models.count也是3),下面是我打印visibelCells的日志。

>>> visibleCells:  <TestCell: 0x7fccbb6a5560; baseClass = UICollectionViewCell; frame = (58969.7 18.4; 353.7 115.2); transform = [0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]; alpha = 0.6; layer = <CALayer: 0x600000ccd9a0>>
>>> visibleCells:  <TestCell: 0x7fccbb74c2b0; baseClass = UICollectionViewCell; frame = (58950 0; 393 128); layer = <CALayer: 0x600000cd8a20>>
>>> visibleCells:  <TestCell: 0x7fccbe06c560; baseClass = UICollectionViewCell; frame = (58989.3 24.8; 314.4 102.4); transform = [0.80000000000000004, 0, 0, 0.80000000000000004, 0, 0]; alpha = 0.2; layer = <CALayer: 0x600000cdd3e0>>
>>> -----------------------
>>> visibleCells:  <TestCell: 0x7fccbb6a5560; baseClass = UICollectionViewCell; frame = (58969.7 18.4; 353.7 115.2); transform = [0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]; alpha = 0.6; layer = <CALayer: 0x600000ccd9a0>>
>>> visibleCells:  <TestCell: 0x7fccbb74c2b0; baseClass = UICollectionViewCell; frame = (58950 0; 393 128); layer = <CALayer: 0x600000cd8a20>>
>>> visibleCells:  <TestCell: 0x7fccbe06c560; baseClass = UICollectionViewCell; frame = (58989.3 24.8; 314.4 102.4); transform = [0.80000000000000004, 0, 0, 0.80000000000000004, 0, 0]; alpha = 0.2; layer = <CALayer: 0x600000cdd3e0>>
>>> -----------------------
>>> visibleCells:  <TestCell: 0x7fccbb6a5560; baseClass = UICollectionViewCell; frame = (59343 0; 393 128); layer = <CALayer: 0x600000ccd9a0>>
>>> visibleCells:  <TestCell: 0x7fccbb663b90; baseClass = UICollectionViewCell; frame = (59382.3 24.8; 314.4 102.4); transform = [0.80000000000000004, 0, 0, 0.80000000000000004, 0, 0]; alpha = 0.2; layer = <CALayer: 0x600000cd6d40>>
>>> visibleCells:  <TestCell: 0x7fccbe06c560; baseClass = UICollectionViewCell; frame = (59362.7 18.4; 353.7 115.2); transform = [0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]; alpha = 0.6; layer = <CALayer: 0x600000cdd3e0>>
>>> -----------------------
>>> visibleCells:  <TestCell: 0x7fccbb74c2b0; baseClass = UICollectionViewCell; frame = (59775.3 24.8; 314.4 102.4); transform = [0.80000000000000004, 0, 0, 0.80000000000000004, 0, 0]; alpha = 0.2; layer = <CALayer: 0x600000cd8a20>>
>>> visibleCells:  <TestCell: 0x7fccbb663b90; baseClass = UICollectionViewCell; frame = (59755.7 18.4; 353.7 115.2); transform = [0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]; alpha = 0.6; layer = <CALayer: 0x600000cd6d40>>
>>> visibleCells:  <TestCell: 0x7fccbe06c560; baseClass = UICollectionViewCell; frame = (59736 0; 393 128); layer = <CALayer: 0x600000cdd3e0>>
>>> -----------------------
>>> visibleCells:  <TestCell: 0x7fccbb55f6f0; baseClass = UICollectionViewCell; frame = (58969.7 18.4; 353.7 115.2); transform = [0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]; alpha = 0.6; layer = <CALayer: 0x600000cc84c0>>
>>> visibleCells:  <TestCell: 0x7fccbb55a1e0; baseClass = UICollectionViewCell; frame = (58950 0; 393 128); layer = <CALayer: 0x600000d68000>>
>>> visibleCells:  <TestCell: 0x7fccbe06c560; baseClass = UICollectionViewCell; frame = (58989.3 24.8; 314.4 102.4); transform = [0.80000000000000004, 0, 0, 0.80000000000000004, 0, 0]; alpha = 0.2; layer = <CALayer: 0x600000cdd3e0>>
>>> -----------------------
>>> visibleCells:  <TestCell: 0x7fccbb55f6f0; baseClass = UICollectionViewCell; frame = (59343 0; 393 128); layer = <CALayer: 0x600000cc84c0>>
>>> visibleCells:  <TestCell: 0x7fccbb6a5560; baseClass = UICollectionViewCell; frame = (59382.3 24.8; 314.4 102.4); transform = [0.80000000000000004, 0, 0, 0.80000000000000004, 0, 0]; alpha = 0.2; layer = <CALayer: 0x600000ccd9a0>>
>>> visibleCells:  <TestCell: 0x7fccbe06c560; baseClass = UICollectionViewCell; frame = (59362.7 18.4; 353.7 115.2); transform = [0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]; alpha = 0.6; layer = <CALayer: 0x600000cdd3e0>>
>>> -----------------------
>>> visibleCells:  <TestCell: 0x7fccbb55a1e0; baseClass = UICollectionViewCell; frame = (59775.3 24.8; 314.4 102.4); transform = [0.80000000000000004, 0, 0, 0.80000000000000004, 0, 0]; alpha = 0.2; layer = <CALayer: 0x600000d68000>>
>>> visibleCells:  <TestCell: 0x7fccbb6a5560; baseClass = UICollectionViewCell; frame = (59755.7 18.4; 353.7 115.2); transform = [0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]; alpha = 0.6; layer = <CALayer: 0x600000ccd9a0>>
>>> visibleCells:  <TestCell: 0x7fccbe06c560; baseClass = UICollectionViewCell; frame = (59736 0; 393 128); layer = <CALayer: 0x600000cdd3e0>>
>>> -----------------------
>>> visibleCells:  <TestCell: 0x7fccbb663b90; baseClass = UICollectionViewCell; frame = (58969.7 18.4; 353.7 115.2); transform = [0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]; alpha = 0.6; layer = <CALayer: 0x600000cd6d40>>
>>> visibleCells:  <TestCell: 0x7fccbb74c2b0; baseClass = UICollectionViewCell; frame = (58950 0; 393 128); layer = <CALayer: 0x600000cd8a20>>
>>> visibleCells:  <TestCell: 0x7fccbe06c560; baseClass = UICollectionViewCell; frame = (58989.3 24.8; 314.4 102.4); transform = [0.80000000000000004, 0, 0, 0.80000000000000004, 0, 0]; alpha = 0.2; layer = <CALayer: 0x600000cdd3e0>>
>>> -----------------------
>>> visibleCells:  <TestCell: 0x7fccbb663b90; baseClass = UICollectionViewCell; frame = (59343 0; 393 128); layer = <CALayer: 0x600000cd6d40>>
>>> visibleCells:  <TestCell: 0x7fccbb55f6f0; baseClass = UICollectionViewCell; frame = (59382.3 24.8; 314.4 102.4); transform = [0.80000000000000004, 0, 0, 0.80000000000000004, 0, 0]; alpha = 0.2; layer = <CALayer: 0x600000cc84c0>>
>>> visibleCells:  <TestCell: 0x7fccbe06c560; baseClass = UICollectionViewCell; frame = (59362.7 18.4; 353.7 115.2); transform = [0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]; alpha = 0.6; layer = <CALayer: 0x600000cdd3e0>>
>>> -----------------------
nukf8bse

nukf8bse1#

你的方法是错误的。不要试图在屏幕上找到一个单元格并改变它的外观。
在收到通知时更新集合视图的模型数据,然后告诉集合视图重新加载受影响的单元格。

相关问题