TableView Cell 内的多个视图

Posted

技术标签:

【中文标题】TableView Cell 内的多个视图【英文标题】:Multiple views inside TableView Cell 【发布时间】:2018-07-11 06:05:54 【问题描述】:

This is how my cell looks like

This is the structuring of my cell

我的 JSON 格式如下所示:


"status": true,
"session": true,
"make": "Ford",
"year": "2015",
"model": "EXPLORER",
"trim": "Base",
"engine": "3.5L V6 - Gas",
"make_id": "96",
"year_id": "100",
"model_id": "284",
"trim_id": "358",
"engine_id": "510",
"title": "Ford 2015 EXPLORER Base 3.5L V6 - Gas - Alternator",
"group_image": "http://www.orouad.com/test_aspns/image/catalog/Ford/67dc394bb5c7839fe09bcbae85210b90.svg",
"parts": [
    
        "product_id": "7760",
        "group_id": "317991",
        "part_number": "DG1Z-10346-B",
        "part_name": "Alternator",
        "part_image": "http://www.orouad.com/test_aspns/image/cache/placeholder-130x130.png",
        "description": "",
        "part_index": "1",
        "quantity": "0",
        "price": "SAR  0.00",
        "after_market": [],
        "superseded": [
            
                "part_id": "11695",
                "part_no": "AA5Z*10346*B",
                "manufacturer": null,
                "description": "Alternator",
                "price": "SAR  0.00",
                "quantity": "0"
            ,
            
                "part_id": "12187",
                "part_no": "GL*980*",
                "manufacturer": null,
                "description": "Alternator",
                "price": "SAR  0.00",
                "quantity": "0"
            
        ]
    ,
    
        "product_id": "7761",
        "group_id": "318048",
        "part_number": "DG1Z-10346-F",
        "part_name": "Alternator",
        "part_image": "http://www.orouad.com/test_aspns/image/cache/placeholder-130x130.png",
        "description": "",
        "part_index": "1",
        "quantity": "0",
        "price": "SAR  0.00",
        "after_market": [],
        "superseded": [
            
                "part_id": "11581",
                "part_no": "8A4Z*10346*A",
                "manufacturer": null,
                "description": "Alternator",
                "price": "SAR  0.00",
                "quantity": "0"
            ,
            
                "part_id": "12032",
                "part_no": "DG1Z*10346*A",
                "manufacturer": null,
                "description": "Alternator",
                "price": "SAR  0.00",
                "quantity": "0"
            ,
            
                "part_id": "12186",
                "part_no": "GL",
                "manufacturer": null,
                "description": "Alternator",
                "price": "SAR  7.00",
                "quantity": "0"
            
        ]
    ,
    
        "product_id": "7762",
        "group_id": "318101",
        "part_number": "GB5Z-10346-C",
        "part_name": "Alternator",
        "part_image": "http://www.orouad.com/test_aspns/image/cache/placeholder-130x130.png",
        "description": "",
        "part_index": "1",
        "quantity": "0",
        "price": "SAR  0.00",
        "after_market": [],
        "superseded": [
            
                "part_id": "11848",
                "part_no": "BL3Z-10346-A",
                "manufacturer": null,
                "description": "Alternator",
                "price": "SAR  0.00",
                "quantity": "0"
            ,
            
                "part_id": "12079",
                "part_no": "DG1Z-10346-C",
                "manufacturer": null,
                "description": "Alternator",
                "price": "SAR  0.00",
                "quantity": "0"
            ,
            
                "part_id": "12186",
                "part_no": "GL",
                "manufacturer": null,
                "description": "Alternator",
                "price": "SAR  7.00",
                "quantity": "0"
            
        ]
    

如果数组“aftermarket”和“superseded”中有任何值,我想显示所有“零件”以及“售后市场”和“取代”零件,否则隐藏售后市场和被取代视图并仅显示零件视图。

我的问题是,如果这些数组中有多个值,我如何显示售后市场的多个视图并被取代。

这是显示售后市场是否只有一个值或已被取代的代码

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return partsData.count


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let cell = tableView.dequeueReusableCell(withIdentifier: "partsDetailCell") as!  VehiclePartsDetailCell
    cell.layer.borderColor = UIColor.white.cgColor
    cell.layer.borderWidth = 1
    let partData = partsData[indexPath.row]
    if (partData.afterMarket?.isEmpty)!
        cell.bottomView.isHidden = true
    else
        cell.bottomView.isHidden = false
        cell.bottomPartLbl.text = partData.afterMarket![0].partNo
        cell.bottomDesLbl.text =  partData.afterMarket![0].description
        cell.bottomPriceLbl.text = partData.afterMarket![0].price
    
    cell.serialNoLbl.text = partData.partIndex
    cell.partNoLbl.text = partData.partNumber
    cell.descriptionLbl.text = partData.partName
    cell.priceLbl.text = partData.price
    cell.cartBtn.setImage(UIImage(named: "cart.png"), for: .normal)
    return cell

我有任何人提供有关如何填充数据或在 tableview 中使用多个单元格的任何建议。

【问题讨论】:

提及您需要的部分数量和 cellForRowAt indexPath: 在每个部分中创建多个单元格。 if indexPath.section = 0 //第一个模型单元格 else if indexPath.section = 1 //显示第二个模型单元格 else //显示第三个模型单元格 我不想创建单独的部分,我只想显示售后市场和被取代的部分,如果有任何价值,或者只是隐藏它们。其他部分也像这样。 【参考方案1】:

这只是伪代码,你必须修改它。 首先,您必须使用此方法设置单元格大小

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
   return currentCellSize + partsData[indexPath.row].superseded.count * supersededSize + afmNormalHight * partsData[indexPath.row].afterMarket.count

之后,您需要将您的单元格创建更改为这样

var currentCount = 0
 for afterMarket in partData.afterMarke 
     let afm = AfterMarketView()
     afm.bottomPartLbl.text = partData.afterMarket![0].partNo
     afm.bottomDesLbl.text =  partData.afterMarket![0].description
     afm.bottomPriceLbl.text = partData.afterMarket![0].price
     afm.frmae = CGFrame(0, normalCellSize + afmNormalHight * currentCount, cell.frame.size.width, afmNormalHight)
    currentCount++
   cell.addSubview(afm)
  

【讨论】:

谢谢,我试试看! 我希望你能理解这个想法。请让我知道是否可以或是否有问题。 currentCount有什么用? 计算单元格中的当前位置后市场。例如,没有 afterMarkets 的单元格将是 50px。第一个后市场在 y 轴上的位置将是 50,第二个应该是 50 + AfterMarketView 的高度,第三个是 50 + 2 * AfterMarketView 的高度。 查看后如何更改标签的文本。目前我将标签附加到单元格(如您在上面的代码中所见)。

以上是关于TableView Cell 内的多个视图的主要内容,如果未能解决你的问题,请参考以下文章

Cell内的TableView动态高度问题

使用 UIView xib 作为 Table View Cell

使用 xib 和 tableView 的多个视图

tableView的section和row

TableView Cell 中的 CollectionView 在 iOS 中无法正常工作

iOS 中 tableView 怎么设置Cell与Cell 之间的间距