0

I already known how to make a basic tableView, below is my basic code

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,46246226301561000110001001", "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 , bundle: nil), 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
            }

but this is an unsafe way because i write func number of rows and cell for row as hardcode. so I want to change tableView composing way from decide cell format first then fill data in (like my basic code) to let data decide my number of rows and cell for row. (use indexPath) but I got some problems:
I try to write:

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

It works but every cell looks the same while I want to present different cells. I search some information on internet, perhaps I can use struct and combine m_arrDescription to make tableview cell.

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

So far this is what i've written, can anyone please kindly help me to go on? Be clearly, how do I use [indexPath.row] in this code ?

AmyLin
  • 1
  • 2
  • This is very confusing. Edit your question and try to explain in plain language what you want to do. – DonMag Oct 22 '20 at 12:49
  • @DonMag Sorry for my poor explanation, I reedit the question, thanks. – AmyLin Oct 23 '20 at 02:06
  • This is still confusing... your post includes `ShoppingTableViewController` ... are you trying to show a list (table view) of "shopping items" and then when a row is selected show the *details* of that item? – DonMag Oct 23 '20 at 15:20

1 Answers1

0

please create first one enum

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

and then use this code in tableview delegate methods.

switch CellType(rawValue: indexPath.row) {
    case .title:
        break
    case .image:
        break
    case .name:
        break
    case .none:
        break
    }

If any problem let me know

  • hi I try to write : switch CellType (rawValue: indexPath.row){} in func cell for row at {}of tableview but Xcode warning me an error: “cannot convert value of type Int to expected argument type String” – AmyLin Oct 22 '20 at 12:17