Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use switch-case in Table View?

Since there are many different cells in the table view, I set up a structure like this. It works correctly and properly, but it’s pretty messy. I want to edit the structure I built using this section using switch – case. How can I do that? Can you show me a way?

func numberOfSections(in tableView: UITableView) -> Int {
        numberOfCardItem() >= 0 ? 9 : 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let loungePeopleCount = numberOfCardItem()
        let hostCount = numberOfHost()
        let attendeesCount = presenter?.getAttendees()?.count ?? 0
        var count: Int = 0
        
        //TODO: switch - case
        if section == EventDetailConstants.AboutTableViewCellTypes.details.rawValue {
            return itemCount()
        } else if section == EventDetailConstants.AboutTableViewCellTypes.about.rawValue && shouldShowAboutCell() {
            return itemCount()
        } else if section == EventDetailConstants.AboutTableViewCellTypes.host.rawValue {
            count += hostCount + 1
        } else if section == EventDetailConstants.AboutTableViewCellTypes.hostBottom.rawValue {
            count += itemCount()
        } else if section == EventDetailConstants.AboutTableViewCellTypes.lounge.rawValue && shouldShowLoungeCell() {
            count += loungePeopleCount + 1
        } else if section == EventDetailConstants.AboutTableViewCellTypes.loungeButton.rawValue && shouldShowLoungeCell() {
            count += itemCount()
        } else if section == EventDetailConstants.AboutTableViewCellTypes.attendees.rawValue {
            count += attendeesCount + 1
        } else if section == EventDetailConstants.AboutTableViewCellTypes.attendeesButton.rawValue {
            count += itemCount()
        } else if section == EventDetailConstants.AboutTableViewCellTypes.tags.rawValue {
            return itemCount()
        }
        return count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let index = indexPath.row
        let section = indexPath.section
        
        //TODO: switch - case
        if section == EventDetailConstants.AboutTableViewCellTypes.details.rawValue {
            return setupDetailsTableViewCell(tableView: tableView)
        } else if section == EventDetailConstants.AboutTableViewCellTypes.about.rawValue && shouldShowAboutCell() {
            return setupAboutTableViewCell(tableView: tableView)
        } else if section == EventDetailConstants.AboutTableViewCellTypes.host.rawValue {
            return index == 0 ? setupTitleTableViewCell(tableView: tableView, section: section, type: .host) : setupHostTableViewCell(tableView: tableView, index: index - titleCellCount)
        } else if section == EventDetailConstants.AboutTableViewCellTypes.hostBottom.rawValue {
            return setupBottomTableViewCell(tableView: tableView)
        } else if section == EventDetailConstants.AboutTableViewCellTypes.lounge.rawValue && shouldShowLoungeCell() {
            return index == 0 ? setupTitleTableViewCell(tableView: tableView, section: section, type: .lounge) : setupLoungePeopleTableViewCell(tableView: tableView, index: index - titleCellCount)
        } else if section == EventDetailConstants.AboutTableViewCellTypes.loungeButton.rawValue && shouldShowLoungeCell() {
            return setupBottomButtonTableViewCell(tableView: tableView, type: .lounge)
        } else if section == EventDetailConstants.AboutTableViewCellTypes.attendees.rawValue {
            return index == 0 ? setupTitleTableViewCell(tableView: tableView, section: section, type: .attendees) : setupAttendeesTableViewCell(tableView: tableView, index: index - titleCellCount)
        } else if section == EventDetailConstants.AboutTableViewCellTypes.attendeesButton.rawValue {
            return shouldShowInviteButton() ? setupBottomButtonTableViewCell(tableView: tableView, type: .invite) : setupBottomTableViewCell(tableView: tableView)
        } else if section == EventDetailConstants.AboutTableViewCellTypes.tags.rawValue {
            return setupMarkerTableViewCell(tableView: tableView)
        } else {
            return UITableViewCell()
        }
    }

Although it works properly, it’s pretty bad in terms of code legibility. I’m trying to return the switch case structure as below, but it gets corrupted

switch indexPath.row {
        case 0:
            ...
        case 1:
            ...
        case 2:
           ...
             }
        case 3:
           ...     
            }
        default:
            return UITableViewCell()
        }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s not clear what EventDetailConstants.AboutTableViewCellTypes is in your code

At least I’m guessing you could use:

let cellType = EventDetailConstants.AboutTableViewCellTypes(rawValue: section)

swith cellType {
case .details: // some code
case .about:   // some code
case .host:    // some code
...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading