Search code examples
uitableviewswiftios8viewwithtag

viewWithTag returns nil for UITableViewCell


I have a UITableView with a prototype UITableViewCell with the identifier contact. In interface builder, I built the cell correctly and have all the elements set to the right tags. When I try to edit a UILabel in the cell that has the tag 101 using cell.viewWithTag, the method returns nil. This seems to only be a problem on iOS 8. I am using storyboards and Swift. Here is the code in tableView:cellForRowAtIndexPath:

let contact = contacts[indexPath.row]
println("Contact: \(contact)")
cell = tableView.dequeueReusableCellWithIdentifier("contact") as UITableViewCell
(cell.contentView.viewWithTag(101) as UILabel).text = contact["name"]! as String

Solution

  • try to use this

    let contact = contacts[indexPath.row]
    println("Contact: \(contact)")
    cell = tableView.dequeueReusableCellWithIdentifier("contact", forIndexPath: indexPath) as UITableViewCell
    (cell.contentView.viewWithTag(101) as UILabel).text = contact["name"]! as String