I'll jump right to it.
I have a cell with a titleLabel that prints a code and a detailTextLabel that prints a decoded version of that code. I have a plist dictionary database containing hundreds of etries like this: "JD" : "John Doe".
When setting the detailTextLabel in cellForRow I initially had a function that looked up the code in the plist and returned the decoded value. This worked, but the scroll was extremely sluggish.
I am now doing the same thing, but this time the decode lookup is happening in the background and returns the result in a callback closure. The scroll is smooth and it works fine. Almost. Sometimes the decoded value is returned for the wrong cell (since it is reused). Is this preventable?
Here is what I have in my cellForRow function:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Create the cell
let cell = tableView.dequeueReusableCellWithIdentifier("RosterCell", forIndexPath: indexPath) as UITableViewCell
if let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as? Object
{
cell.textLabel?.text = object.rawCode
CodeDecoder.getDescriptionInBackground(CodeType.All, code: rawCode) { (description) -> () in
if description.isEmpty == false {
cell.detailTextLabel?.text = description
}
else {
cell.detailTextLabel?.text = "No decode found for code: \(object.rawCode)."
}
}
}
else {
cell.textLabel?.text = "Object not found."
cell.detailTextLabel?.text = ""
}
return cell
}
I would greatly appreciate any help or ideas around this.