Let's say I have an app that displays details for a person. Each person can have zero or more phone numbers and zero or more notes attached to it.
Thus, I have a Core Data entity Person with one-to-many relationships to the Phone entity and the Note entity.
I want to show these in a UITableView, where there is one section "phones" and one section "notes".
So, numberOfRowsInSection would look like that:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0: // Phones
if self.person.phones.count > 0 {
return self.person.phones.count
} else {
return 1 // We want a different cell to display "no phones"
}
case 1: // Notes
if self.person.notes.count > 0 {
return self.person.notes.count
} else {
return 1 // We want a different cell to display "no notes"
default:
return 0
}
}
And cellForRowAt would look like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
if self.person.phones.count > 0 {
return UITableViewCell.init(style: .default, reuseIdentifier: "PhoneNumberCell")
} else {
return UITableViewCell.init(style: .default, reuseIdentifier: "NoPhoneNumbersCell")
}
case 1:
if self.person.notes.count > 0 {
return UITableViewCell.init(style: .default, reuseIdentifier: "NoteCell")
} else {
return UITableViewCell.init(style: .default, reuseIdentifier: "NoNotesCell")
}
default:
return UITableViewCell.init(style: .default, reuseIdentifier: "default")
}
}
And then, you can guess already, the same code would repeat for willDisplay:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
switch indexPath.section {
case 0:
if self.person.phones.count > 0 {
// Configure Phone Cell
} else {
// Configure No Phones Cell
}
case 1:
if self.person.notes.count > 0 {
// Configure Note Cell
} else {
// Configure No Notes Cell
}
default: break
}
}
Same would apply for didSelectRowAt (and other delegate/datasource methods) but I won't copy more here.
What I want to do is get rid of this long and repeating switch with if..else statement but the problem is that in some cases I use it to return a number, in others a String and in others to just configure a cell.
Does anyone have an idea or pattern to recommend for this case?