我正在使用与 TableView 连接的 ViewController 并使用 ExpandableCell lib 来展开 UITableviewcell。
我在 Storyboard中使用 UICollectionview 创建了一个扩展单元,但是当我使用 dequeueReusableCell 时,他不会加载 UICollectionview
我使用的是 Swift 4 Xcode 9.2
这是我在 View Controller 的代码
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(ServicesCell.self, forCellReuseIdentifier: "servicesCell")
self.tableView.register(ConciergeCell.self, forCellReuseIdentifier: "conciergeCollection")
}
extension ViewController : ExpandableDelegate {
func expandableTableView(_ expandableTableView: ExpandableTableView, expandedCellsForRowAt indexPath: IndexPath) -> [UITableViewCell]? {
let cell = tableView.dequeueReusableCell(withIdentifier: ConciergeCell.ID) as! ConciergeCell
return [cell]
}
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return concierge.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch indexPath.section {
cell.displayContent(image: concierge[indexPath.row].image!, title: concierge[indexPath.row].name!)
cell.backgroundColor = UIColor.dark70
cell.layoutSubviews()
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
}
UITableviewcell 类
class ConciergeCell: UITableViewCell {
static let ID = "conciergeCollection"
@IBOutlet weak var collectionView: UICollectionView!
}
UICollectionviewcell
class ConciergeCollectionCell: UICollectionViewCell {
@IBOutlet var mainImage: UIImageView!
@IBOutlet var mainLabel: UILabel!
func displayContent(image: UIImage, title: String) {
mainImage.image = image
mainLabel.text = title
}
}
我可以采取哪些步骤来解决这个问题?我不知道是否有必要在 viewDidLoad 中注册 uitableviewcell 因为我是使用 Storyboard 创建的
最佳答案
问题是,您没有为 UICollectionView 实例设置委托(delegate)和数据源。
此图中突出显示的部分表明它们未连接。
在您的 ConciergeCell
类中,进行以下更改:
class ConciergeCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
static let ID = "conciergeCollection"
@IBOutlet weak var collectionView: UICollectionView!
private var concierge: Concierge
func awakeFromNib() {
super.awakeFromNib()
self.collectionView.delegate = self
self.collectionView.dataSource = self
}
func configure(withConciergeData concierge:Concierge) {
self.concierge = concierge
self.concierge.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.concierge.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell.displayContent(image: self.concierge[indexPath.row].image!, title: concierge[indexPath.row].name!)
cell.backgroundColor = UIColor.dark70
cell.layoutSubviews()
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
}
}
然后,在以下函数中,在返回 [cell]
数组之前,使用礼宾数据的实例调用 configure(withConciergeData:)
函数,如下所示所以:
extension ViewController : ExpandableDelegate {
func expandableTableView(_ expandableTableView: ExpandableTableView, expandedCellsForRowAt indexPath: IndexPath) -> [UITableViewCell]? {
let cell = tableView.dequeueReusableCell(withIdentifier: ConciergeCell.ID) as! ConciergeCell
cell.configure(withConciergeData: concierge)
return [cell]
}
}
请注意,我在这里输入了这段代码。如果将其复制并粘贴到 Xcode 中,可能会出现一些编译器错误。希望这会有所帮助。
关于ios - UITableviewcell 不显示 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49825536/