Home > Software engineering >  How to use switch-case in Table View?
How to use switch-case in Table View?

Time:08-08

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()
        }

CodePudding user response:

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
...
}
  • Related