通过indexpath使tableView单元具有结构和数据

问题描述

我已经知道如何制作基本的tableView,下面是我的基本代码

class ShoppingTableViewController: UITableViewController{
var description = ["D1","299900","D2","P201712310000003000","D3","ASS+DfDFxSuu","D10","901","D11","00,46246226301561000110001001","D12","20201231123030"]
var dictDescription = ["D10": "901","D11": "00,"D3": "ASS+DfDFxSuu","D12": "20201231123030","D2": "P201712310000003000","D1": "299900"]

override func viewDidLoad() {
        super.viewDidLoad()
// Xib
        tableView.register(UINib(nibName:PropertyKeys.pictureCell,bundle: nil),forCellReuseIdentifier: PropertyKeys.pictureCell)
        tableView.register(UINib(nibName:PropertyKeys.infoCell,forCellReuseIdentifier: PropertyKeys.infoCell)
}

 override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return 4
    }
 override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 switch indexPath.row {
case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: PropertyKeys.pictureCell,for: indexPath) as! PictureWithTableViewCell
            cell.iconImageView.image = UIImage(named: "amount")
            cell.lbDescription.text = "Type"
            cell.lbName.text = "Shopping"
            return cell
case 1:
             let cell = tableView.dequeueReusableCell(withIdentifier: PropertyKeys.infoCell,for: indexPath) as! InfoTableViewCell
                cell.lbDescription.text = "Taiwan dollar"
                cell.lbName.text = m_dictDescription["D1"]
                return cell
case 2:
            let cell = tableView.dequeueReusableCell(withIdentifier: PropertyKeys.pictureCell,for: indexPath) as! PictureWithTableViewCell
            cell.iconImageView.image = UIImage(named: "info")
            cell.lbDescription.text = "BankName"
            cell.lbName.text = m_dictDescription["D11"]
            return cell
case 3:
            if m_dictDescription["D2"] != nil {
                let cell = tableView.dequeueReusableCell(withIdentifier: PropertyKeys.infoCell,for: indexPath) as! InfoTableViewCell
                cell.lbDescription.text = "orderNumber"
                cell.lbName.text = m_dictDescription["D2"]
                return cell
            }else {
                let cell = tableView.dequeueReusableCell(withIdentifier: PropertyKeys.infoCell,for: indexPath) as! InfoTableViewCell
                cell.isHidden = true
                return cell
            }

但这是一种不安全的方法,因为我将功能行和单元格的函数写为硬编码。 所以我想改变tableView的组成方式,首先是确定单元格格式,然后再填充数据(如我的基本代码),以使数据确定行数和行单元格。 (使用indexPath) 但是我遇到了一些问题:
我试着写:

override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let test = self.m_arrDescription[indexPath.row]
cell.lbName.text = test

它可以工作,但是每个单元格看起来都一样,而我想展示不同的单元格。 我在互联网上搜索了一些信息,也许我可以使用struct并结合使用m_arrDescription来创建表格视图单元。

// use struct to decide cell label or image,for example: cell.lbDescription.text ...
struct CellFormat {
        var title : String
        var image : UIImage
        var name : String
    }

到目前为止,这是我所写的内容,任何人都可以请我帮忙吗? 清楚一点,如何在此代码中使用[indexPath.row]?

解决方法

请创建第一个枚举

Traceback (most recent call last):
  File "C:/Users/admin/Desktop/delet.py",line 4,in <module>
    engine.setProperty('voice',voices[1].id) #changing index changes voices but ony 0 and 1 are working here
IndexError: list index out of range

,然后在tableview委托方法中使用此代码。

enum CellType: String,CaseIterable {
     case title,image,name
}

如果有任何问题让我知道

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...