1

I think I'm missing something fundamental to Swift, but can't find other examples of people doing what I'm trying:

Background

I have a UITableView with 2 prototype cells, with a different identifies, different features (label, image etc) and different classes.

I want the cellForRowAt function to return a different type and class of cell depending on the content in the array that holds the table data. That array is populated with struct instances, one characteristic of which identifies the type of cell that I want to represent the data.

Code Attempt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       switch dataArray[indexPath.item].typeOfData {
       case "Type1":
          let cell = tableView.dequeueReusableCell(withIdentifier: "type1ReuseIdentifier", for: indexPath) as! type1Cell
          //Set up the cell contents
          return cell
       case "Type2":
          let cell = tableView.dequeueReusableCell(withIdentifier: "type2ReuseIdentifier", for: indexPath) as! type2Cell
          //Set up the cell contents
          return cell
       default:
          let cell = tableView.dequeueReusableCell(withIdentifier: "separatorIdentifier", for: indexPath) as! separatorCell
          //Set up the cell contents
         cell
     }
}

What am I missing / doing wrong?


Edit: It was missing the final return.

SimonMoss
  • 310
  • 4
  • 12
  • Have you written `default` case? – iPeter Jul 17 '17 at 14:54
  • Compare [Missing return UITableViewCell](https://stackoverflow.com/questions/30189505/missing-return-uitableviewcell). – Martin R Jul 17 '17 at 14:57
  • Sorry, yes the default statement is there as well and returns a separator type cell – SimonMoss Jul 17 '17 at 14:59
  • 2
    *"Swift wants me to declare and return the cell outside of the switch statement"* – No, I don't think so. If all cases in the switch statement return a cell then it should compile without problems. – Martin R Jul 17 '17 at 15:03
  • I was being an idiot, you're right Martin, one of my switch cases was missing the return! – SimonMoss Jul 17 '17 at 15:06

1 Answers1

2

just do like this:

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

    let typeSection = CellType(rawValue: indexPath.row)!

    switch typeSection {
    case .CenterTitleCell:
        return getCenterTitleCell(tableView, indexPath)
    case .CenterDescriptionCell:
        return getCenterDescriptionCell(tableView, indexPath)
    case .CenterImageCell:
        return getImageCell(tableView, indexPath)
    case .FooterTitleCell:
        return getFooterViewCell(tableView, indexPath)
    }
}

And use another method to return the Cell Type

func getCenterTitleCell (_ tableView: UITableView, _ indexPath:IndexPath) -> CenterTitleTableViewCell {
    let cell:CenterTitleTableViewCell =  tableView.dequeueReusableCell(withIdentifier: String(describing: CenterTitleTableViewCell.self),
                                                                                   for: indexPath) as! CenterTitleTableViewCell
    cell.selectionStyle = .none
    return cell
}
Pedro Pinho
  • 652
  • 3
  • 6
  • This assume there are only 4 rows and each row gets its own cell type. In general, this is not how most tables would be setup. – rmaddy Jul 17 '17 at 15:41
  • This is a simple example how you can do it. In this case, i have a enum providing cell type because it's a static screen. Should adapt the example to the code he wants. – Pedro Pinho Jul 17 '17 at 18:17