3

Any suggestions regarding factoring a massive view controller, with multiple cell types, cell sizes. Because each cell has its own delegates, the view controller becomes super confusing when navigating through the file.

Or is this the standard approach to for multiple cell types? How would you go about building multiple with multiple sections & cells. Much thanks!

//Rows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if section == 0 && sectionArray[0].isExpanded {
        return sectionArray.count
    } else if section == 0 && sectionArray[0].isExpanded == false {
        return sectionArray[0].section.count - 1
    }

    if section == 1 {
        return 3
    }
    return 0
}

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

    if indexPath.section == 0 && indexPath.row == 0 {

        let cell = tableView.dequeueReusableCell(withIdentifier: "CreateFavoriteIconCell", for: indexPath) as! CreateFavoriteIconCell
        cell.delegate = self
        return cell
    }

    if indexPath.section == 1 {
        switch indexPath.row {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "CreateFavoritePhotoCell", for: indexPath) as! CreateFavoritePhotoCell
            cell.delegate = self
            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "CreateFavoriteLocationCell", for: indexPath) as! CreateFavoriteLocationCell
            return cell
        case 2:
            let cell = tableView.dequeueReusableCell(withIdentifier: "CreateFavoriteDescriptionCell", for: indexPath) as! CreateFavoriteDescriptionCell
            return cell
        default:
            return tableView.cellForRow(at: indexPath)!
        }
    }
    return tableView.cellForRow(at: indexPath)!
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.section == 0 {
        switch indexPath.row {
        case 0:
            tableView.rowHeight = 58
        default:
            tableView.rowHeight = 0
        }
    }

    if indexPath.section == 1 {
        switch indexPath.row {
        case 0:
            tableView.rowHeight = 120
        case 1:
            tableView.rowHeight = 60
        case 2:
            tableView.rowHeight = 160
        default:
            tableView.rowHeight = 0
        }
    }

    return tableView.rowHeight
}
Kenny Ho
  • 409
  • 3
  • 16
  • 1
    Sorry. It’s actually about tableView. Here it is again: https://medium.com/chili-labs/configuring-multiple-cells-with-generics-in-swift-dcd5e209ba16 – Galo Torres Sevilla Feb 06 '19 at 23:20
  • Could you please provide more details of your datasource and how you want your cell to look like? – Tieda Wei Feb 07 '19 at 01:05
  • Hi Wei, I can try to share with you an image. But am having difficulty with embedding an image from pc to the question. The new UI for questions is super confusing. By any chance you know how? – Kenny Ho Feb 07 '19 at 02:45
  • You can find answer here - https://stackoverflow.com/a/64461498/6570914 – Denis Kakačka Oct 21 '20 at 19:55

0 Answers0