Reputation: 146
I cannot understand why some values are well passed from the tableViewController
to my custom UITableViewCell
, and some others are not.
In my UItableviewController.cellForRowAtIndexPath
i set up a cell with some values, before returning this cell :
cell.label1.text = myCustomObject.id
cell.label2.text = myCustomObject.path
cell.myCustomObjectCellMember = myCustomObject
cell.pathCellMember = myCustomObject.path
return cell
On the custom UITableViewCell
side, in awakeFromNib
method, the two first cell members are Ok, the two last ones contain nil
.
The only difference between the two first cell members and the two last ones is that the two first are declared as IBOutlet
and linked to the storyboard, while the two others are not linked to the UI. But yet, it should be OK to write in these vars from the tableViewController
, right ?
Here are the declarations of these variables in the custom UITableViewCell
:
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
var pathCellMember : String!
var myCustomObjectCellMember: MyCustomObjectCellMember!
When logged (inside UITableViewCell.awakeFromNib
), label1.text
and label2.text
show the correct value,
but pathCellMember
and myCustomObjectCellMember
display nil
instead of the value assigned in UItableviewController.cellForRowAtIndexPath
.
As requested, a more explicit code :
class CustomCellTableViewCell: UITableViewCell {
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
var pathCellMember : String!
var myCustomObjectCellMember: MyCustomObjectCellMember!
override func awakeFromNib() {
super.awakeFromNib()
println("label1 : \(self.label1.text!)") //displays the value assigned
println("label2 : \(self.label2.text!)") //displays the value assigned
println("pathCellMember: \(self.pathCellMember!)") //displays nil
println("myCustomObjectCellMember.path : \(self.myCustomObjectCellMember.path)") //displays `nil`
}
Thank you
Upvotes: 0
Views: 974
Reputation: 9414
In cellForRowAtIndexPath is where you will reuse (deque) each cell. This is where you need to assign your vars values to each cell. While awake does set the initial values they will only fire the first time before the cell is reused. Assign everything in cellForRow or willDisplayCell (background colors, etc).
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/
Discussion For performance reasons, a table view’s data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView:cellForRowAtIndexPath: method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse. Call this method from your data source object when asked to provide a new cell for the table view. This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. If no cell is available for reuse and you did not register a class or nib file, this method returns nil. If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.
Upvotes: 2