TableView contents are not tagged in row 0

Hi,

this is my first post here. I am doing the “Beginning Table Views” course on ios12 and implementing a TableViewController.

Problem: I’m tagging a View inside my TableView controller in UIBuilder and then access it by tag in the controller code. This strangely this works in all but one case. The contents of row 0 row of the TableView do not downcast to UILabel. I end up with one row in my output that carries the std. “Label”

Consider this controller for Exercise 7:

` override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) → UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: “ChecklistItem”, for: indexPath)

// tableView.viewWithTag accesses the inner with with a tag
print(indexPath.row)
if let label = tableView.viewWithTag(1000) as? UILabel {
switch indexPath.row % 5 {
case 0:
label.text = “Take a Jog”
case 1:
label.text = “Watch a movie”
case 2:
label.text = “Code an app”
case 3:
label.text = “Walk the dog”
case 4:
label.text = “Study Design”
default:
label.text = “unassigned”
}
}
return cell
}`

why would

‘if let label = tableView.viewWithTag(1000) as? UILabel’

hold true for all but the first row out of ten?

I think you want to use

if let label = cell.viewWithTag(1000) as? UILabel {

Otherwise you aren’t being specific about which row you want. If you have 5 rows, the tableview has 5 views with tag 1000. You may just be getting lucky on the ones that work.

Hi Steve,

indeed that was the bug, thanks for your help! It picked random Labels instead of the cell’s label and i didn’t notice.

Simon

This topic was automatically closed after 166 days. New replies are no longer allowed.