Dealing with Static UITableView’s

Derrick Ho
2 min readDec 24, 2016

UITableViews are the bread and butter of all iOS developers. If you don’t know them, you can’t call yourself an iOS developer (yet).

UITableViews are great for showing dynamic content in an efficient way, but what happens when you are dealing with static content?

class OldViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView = UITableView()
var array = ["Apple", "Bananna"]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: nil)
if indexPath.row == 0 {
cell.textLabel!.text = array[indexPath.row]
} else if indexPath.row == 1 {
cell.textLabel!.text = array[indexPath.row]
}
return cell
}
}

You may have created something similar to the above. You have a static array of fruits and your trusty tableView(_:cellforRowAt:) method. Almost immediately there is a problem.

Magic Numbers

My professors, Brian Linard and Kris Miller would villainize the humble magic number. There were many digital witch hunts to snuff out the casters of magic. Those were dark times. While the witch hunts are now mostly in the past, the stigma against these magical values remains.

I suppose in small quantities, no one will notice. But lets add another method to our class.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
print("pressed Apple")
} else if indexPath.row == 1 {
UIAlertView(title: nil, message: "pressed bananna", delegate: nil, cancelButtonTitle: "ok").show()
}
}

Again, magic numbers are used. If my data source array were to change, there is a chance that this logic will need fixing. There are many more delegate methods and you would have to repeat this pattern over and over again.

Does it have to be this way?

Couldn’t we bundle up the logic for each cell? Imagine writing this…

tableView.dataSourceArray = [
GTVSection()
.setCells([
GTVCell()
.setBlock_cellForRowAtIndexPath({ _ in
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel!.text = "Apple"
return cell
})
.setBlock_didSelectRowAtIndexPath({ _ in
print("pressed Apple")
})
,
GTVCell()
.setBlock_cellForRowAtIndexPath({ _ in
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel!.text = "Banana"
return cell
})
.setBlock_didSelectRowAtIndexPath({ _ in
UIAlertView(title: nil, message: "pressed bananna", delegate: nil, cancelButtonTitle: "ok").show()
})
])
]

No magic numbers in sight.

For implementation details checkout the github repository.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Responses (1)

Write a response

Nice! Thanks for the insight!

--