TableView单元内的多个视图

时间:2018-07-11 06:05:54

标签: ios swift uitableview uiview

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"
            }
        ]
    }

我想显示所有“零件”以及“零件市场”和“被取代的”零件,如果数组“零件市场”和“被取代的”内有任何价值,否则隐藏零件市场和被取代的视图,只显示零件视图。

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

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

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中的多个单元格提供任何建议。

1 个答案:

答案 0 :(得分:0)

这是伪代码,例如,您必须对其进行修改。 首先,您必须使用此方法设置像元大小

 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)
  }