【问题标题】:Populating dynamic data in CollectionView which is inside TableView在 TableView 内的 CollectionView 中填充动态数据
【发布时间】:2020-08-26 12:06:11
【问题描述】:

主视图控制器

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
       }

输出here is my output I want to display data like first section of 2D array under t1(Category 1) and second section under t2(category 2). both categories and items in collection view are dynamic

【问题讨论】:

  • 将 CollectionView 放在 TableView 中不是一个好主意,您可以使用 CollectionView 仅使用部分和布局来实现这一点。

标签: ios swift uitableview uicollectionview


【解决方案1】:

您需要在 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 内部调用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多