I am building this pet project with UIKit. The App's main goal is to keep track of my daily expenses.
The AddExpenseViewController
is responsible for capturing user input, parse it into a Expense
instance, and send it back to the UIViewController containing all my expenses using the delegate pattern.
To capture user input I am using a UITableView with two custom cells containing UITextFields: ExpenseTitleTableViewCell
and ExpenseValueTableViewCell
. As this is supposed to work as a form, it's not possible to add new cells (i.e fields) to the UITableView.
Currently, I instantiate the cells in the tableView(_ tableView:, cellForRowAt:) -> UITableViewCell
method using helper functions, like so:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0: return configureTitleCell()
case 1: return configureValueCell()
default: return UITableViewCell()
}
func configureTitleCell() -> UITableViewCell {
let cell = ExpenseTitleTableViewCell(style: .value1, reuseIdentifier: ExpenseTitleTableViewCell.reuseIdentifier)
cell.configureCell()
return cell
}
func configureValueCell() -> UITableViewCell {
let cell = ExpenseValueTableViewCell(style: .value1, reuseIdentifier: ExpenseValueTableViewCell.reuseIdentifier)
cell.configureCell()
return cell
}
A few things look off to me:
First: the view controller is responsible for instantiating the cells, and this generates duplicate code on
configureTitleCell
andconfigureValueCell
. Should I use a pattern like Factory to split responsibilities?Second: having the constant
2
innumberOfRowsInSection
also looks unsafe to me