如何解决在TableView内部的CollectionView中填充动态数据
主视图控制器
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return CategoryNames.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath as IndexPath) as! CategoryTableViewCell
cell.lblCategoryName.text = CategoryNames[indexPath.row] as? String
cell.ViewCategogyBackground.layer.cornerRadius=20
cell.ViewCategogyBackground.layer.maskedCorners = [.layerMaxXMinYCorner]
cell.arrayForCollectionView = [["xx","yy"],["zz","vv","tt"],["hello"],["11","44"]]
cell.reloadCollectionView()
return cell
}
表格视图单元格
var arrayForCollectionView : [[String]]!
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
if (arrayForCollectionView != nil) {
return arrayForCollectionView.count
}
return 0
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell",for: indexPath) as! CategoryItemCollectionViewCell
let rowValue = arrayForCollectionView[indexPath.row]
for i in 0..<rowValue.count {
cell.lblItemName.text = rowValue[i] as? String
}
return cell
}
解决方法
您需要在TableView内部创建CollectionView并在tableViewCell内部创建collectionView的出口,如下所示:
class TimingSlotsTVCell: UITableViewCell
{
@IBOutlet weak var timeSlotsCV: UICollectionView!
}
现在在tableView的cellForRow中执行以下操作:
let cell = tableView.dequeueReusableCell(withIdentifier: "TimingSlotsTVCell") as! TimingSlotsTVCell
cell.timeSlotsCV.reloadData()
return cell
现在collectionView单元格将从tableView内部调用
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。