簡體   English   中英

TableView單元內的多個視圖

[英]Multiple views inside TableView 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中使用多個單元格的建議。

例如,這是偽代碼,您必須對其進行修改。 首先,您必須使用此方法設置像元大小

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM