I have a simple model E-Numbers and it reads from a .csv file. I cache the data into a variable eNummers in my ViewController so I can do basic array operations, such as eNummers[0], eNummers[1].
I read from the array in tableView:cellForRowAtIndexPath, but when I scroll the tableView it is really really slow and laggy. How can I optimize this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("globalCell") as UITableViewCell!
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "globalCell")
}
if (self.eNummers?.getAllNummers().rows[indexPath.row] != nil) {
cell.textLabel?.text = self.eNummers?.getAllNummers().rows[indexPath.row][0]
cell.detailTextLabel?.text = self.eNummers?.getAllNummers().rows[indexPath.row][1]
switch (self.eNummers?.getAllNummers().rows[indexPath.row][2])! {
case "GOOD":
cell.textLabel?.textColor = UIColor.greenColor()
break
case "MODERATE":
cell.textLabel?.textColor = UIColor.orangeColor()
break
case "BAD":
cell.textLabel?.textColor = UIColor.redColor()
break
default:
cell.textLabel?.textColor = UIColor.blackColor()
break
}
}
return cell
}
getAllNummers()do?func getAllNummers() -> CSwiftV { let nummers = CSwiftV(String: self.contents as! String) return nummers }getAllNummersonce and then just access the array directly incellForRowAtIndexPath. Also, if you have registered your cell class or are using a storyboard thendequeueReusableCellWithIdentifiercan't return. Il - this would seem to be the case since you have usedas!which will throw an exception if nil is returned.