![](/img/trans.png)
[英]How to implement Custom UICollectionViewCell using Swift in iOS
[英]How to create custom UICollectionViewCell using Storyboard in iOS?
我有一個UIViewController
中,我有一個UICollectionView
中的.The細胞UICollectionView
必須是圖像,所以我明白,我需要繼承UICollectionViewCell
類。 因此,基本上,我需要一個UIViewController,該UIViewController的UICollectionView帶有一個自定義UICollectionViewCell。 我正在使用情節提要,並且在需要創建自定義UITableVieCell
,請執行以下操作:
UITableViewCell
的子類 UITableViewCell
中的UITableViewCell
的類 但是當我對UICollectionViewCell
遵循相同的UICollectionViewCell
, 例如。 我創建一個名為CustomCollectionViewCell
的子類,在cellForItemAtIndexPath
聲明它
CustomCollectionViewCell *cell = (CustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSLog(@"%@",cell);
這會記錄<UICollectionViewCell: 0x137529010; frame = (10 10; 300 164); layer = <CALayer: 0x170220880>>
<UICollectionViewCell: 0x137529010; frame = (10 10; 300 164); layer = <CALayer: 0x170220880>>
<UICollectionViewCell: 0x137529010; frame = (10 10; 300 164); layer = <CALayer: 0x170220880>>
不管我做什么。 我無法初始化我的自定義UICollectionViewCell
類,因此無法添加圖像。
我查看了其他StackOverflow答案,並閱讀了答案,說不這樣做:
[self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
如果我使用情節提要,那么我將其注釋掉。
所有重用標識符,出口等均已設置。 我似乎找不到錯誤所在。
任何幫助表示贊賞。
嘗試這個,
在Identity Inspector
設置您的自定義collectionview單元格類,例如
並在cellForItemAtIndexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
NSLog(@"%@", cell);
return cell;
}
並獲得日志為
<CollectionViewCell: 0x7af70000; baseClass = UICollectionViewCell; frame = (0 0; 70 69); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7af70210>>
我想到了。
這是一個愚蠢的錯誤。 我正在使用的重用標識符已被其他地方的其他單元使用。 對其進行了更改,現在可以完美運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.