xrs3
xrs3

Reputation: 37

How to create individual tableviews for items in an array?

My app has a number of rooms in a tableview, pictures below, when the user taps on a room, it segue's to a separate view which is also a table view , pictured below.

I would like to display individual checklists within each of these views, however the code I have entered does not display the array items, see below:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (roomCheckList.count)
}

private func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell1")
    
    cell.textLabel?.text = roomList[indexPath.row]
    cell.accessoryType = .disclosureIndicator
    return cell
    
}

How do I create individual checklists for each item when the user selects it from the main screen?

Once created, how do I save these items, so when the user revisits the screen, they have not disappeared?

Home Screen

Individual view

Upvotes: 1

Views: 53

Answers (2)

小村祐輝
小村祐輝

Reputation: 62

Considering maintainability, how about this approach?

enum CellType: Int {
  case aCell
  case bCell
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  guard let cellType = CellType(rawValue: indexPath.row) else { return UITableViewCell() }
  switch cellType {
    case .aCell:
      // configure the Acell

    case .bCell:
      // configure the Bcell

    default
       // configure the other cells
  }
}

Upvotes: 1

Preefix
Preefix

Reputation: 273

Another approach which I saw in different projects: configure each cell based on its index in cellForRowAt()

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   if indexPath.row == 0 {
        //configure the cell
   } else if indexPath.row == 1 {
        //configure the cell
   }
   // configure the other cells
}

Upvotes: 0

Related Questions