首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TableView单元内的多个视图

TableView单元内的多个视图
EN

Stack Overflow用户
提问于 2018-07-11 06:05:54
回答 1查看 467关注 0票数 0

我的手机是这样的

这是我牢房的结构

我有一个JSON格式,如下所示:

代码语言:javascript
运行
复制
{
"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"
            }
        ]
    }

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

我的问题是如何显示后置市场的多个视图,如果这些数组中有多个值,则如何替代。

--这是要显示的代码,如果在售后市场中只有一个值或取代了

代码语言:javascript
运行
复制
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
}

任何人都可以提供关于如何填充数据或在表视图中使用多个单元格的任何建议。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-11 06:54:31

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

代码语言:javascript
运行
复制
 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
   return currentCellSize + partsData[indexPath.row].superseded.count * supersededSize + afmNormalHight * partsData[indexPath.row].afterMarket.count
}

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

代码语言:javascript
运行
复制
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)
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51278269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档