0

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
    }
4
  • What does getAllNummers() do? Commented Jul 25, 2015 at 10:40
  • func getAllNummers() -> CSwiftV { let nummers = CSwiftV(String: self.contents as! String) return nummers } Commented Jul 25, 2015 at 10:41
  • Put the numbers into an array. Commented Jul 25, 2015 at 10:46
  • You should make the call to getAllNummers once and then just access the array directly in cellForRowAtIndexPath. Also, if you have registered your cell class or are using a storyboard then dequeueReusableCellWithIdentifier can't return. Il - this would seem to be the case since you have used as! which will throw an exception if nil is returned. Commented Jul 25, 2015 at 11:06

1 Answer 1

3

Your function getAllNummers() is loading a CSV file and parsing it 4 times for every cell. This will be your big slow down. You should load and parse the file once and store the values in a property and then use the property to access the values you need

Sign up to request clarification or add additional context in comments.

1 Comment

This solved my problem, thank you very much! :) To clarify: I added the parsing part to the init constructor of my model, then called a property in tableView.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.