SPa*_*tel 6 xib ios uicollectionview uicollectionviewcell swift
我有 FeedCell.swift 和 FeedCell.xib,在 feedcell xib 中,我将单元格自定义类设置为“FeedCell”
视图控制器 viewDidLoad 中的代码
collectionView.register(UINib(nibName: "FeedCell", bundle: .main), forCellWithReuseIdentifier: "feedCell")
Run Code Online (Sandbox Code Playgroud)
问题:
我想继承 FeedCell 并将该类与 collectionView 一起使用,例如:
class FeedCell:UICollectionViewCell {
@IBOutlet weak var showImageView: UIImageView!
@IBOutlet weak var showIconImageView: UIImageView!
}
class AppFeedCell: FeedCell {
override func awakeFromNib() {
super.awakeFromNib()
// configure cell
}
}
Run Code Online (Sandbox Code Playgroud)
如何在 collectionview 中注册/使用 FeedCell 和 AppFeedCell?
Est*_*ejo 12
如果你的AppFeedCell有不同的 UI 配置,例如它没有绑定一些在FeedCell 中定义的IBOutlets或者它添加了它的超类中不存在的新的,那么你必须注册两个 Nib(假设你有两个单独的 Nib 文件)
collectionView.register(UINib(nibName: "FeedCell", bundle: .main), forCellWithReuseIdentifier: "feedCell")
collectionView.register(UINib(nibName: "AppFeedCell", bundle: .main), forCellWithReuseIdentifier: "appFeedCell")
Run Code Online (Sandbox Code Playgroud)
然后,您将能够在需要时将每一个出列。
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if you_need_AppFeedCell {
let cell : AppFeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("appFeedCell", forIndexPath: indexPath) as! AppFeedCell
return cell
} else
let cell : FeedCell = collectionView.dequeueReusableCellWithReuseIdentifier("feedCell", forIndexPath: indexPath) as! FeedCell
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
这样,即使AppFeedCell子类化FeedCell ,您也可能有两个不同的 Nib 文件。
否则,如果类和子类共享相同的单元格布局和出口,那么只需将出列的单元格 ( FeedCell ) 转换为AppFeedCell就足够了,而无需注册另一个 Nib,正如上面Taras Chernyshenko指出的那样。
小智 0
在您的单元 xib/storyboard 中,转到身份检查器集类:AppFeedCell
现在,在 CollectionView 的数据源方法中,您可以创建 AppFeedCell 类型的单元格。
| 归档时间: |
|
| 查看次数: |
12089 次 |
| 最近记录: |