uhfocuz
uhfocuz

Reputation: 3180

UITableView cellForRowAtIndexPath issue with using a reuse identifier

My table sets up perfectly and as I scroll through it and it calls cellForRowAtIndexPath to load the actual rows, it has this second of lag where it freezes and causes the table to not be fluent. I did some research on why this is, and as far as my understanding goes, it's because I am not reusing cells. I program in swift and I'm also fairly new. I was wondering if anyone could help fix my Swift code to reuse cells so that when I scroll through the table its fluent and lag free.

Here is my code:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        mainTableView.estimatedRowHeight = 50.0
        mainTableView.rowHeight = UITableViewAutomaticDimension
        var cell : postWithTitleAndImageTableViewCell = mainTableView.dequeueReusableCellWithIdentifier("postWithTitleAndImage") as postWithTitleAndImageTableViewCell
        var imageID = json[indexPath.row]["imageName"] as String
        var id = json[indexPath.row]["id"] as String
        var isImage = json[indexPath.row]["isImage"] as String
        var title = json[indexPath.row]["name"] as String
        var text = json[indexPath.row]["post"] as String
        var time = json[indexPath.row]["time"] as String
        var popularity = json[indexPath.row]["popularity"] as String
        //add clientid
        //time handle in the cell class
        lastID = id
        cell.setCell(id, title: title, imageID: imageID, text: text, time: time, popularity: popularity)
        return cell as postWithTitleAndImageTableViewCell
    }

If I need to provide any more information, please let me know. It's really important that I get this fixed.

EDIT: I have found where the performance issue is in my code. In my custom cell class there is a setCell function to set everything in the cell. In this code I had a web request to get an image. This is a necessary call so I'd like to solve the performance issue.

Here is setCell:

    public func setCell(id: String, title: String, imageID: String, text: String, time: String, popularity: String){
        println("setCell called")
        readIDs.append(id)
        postID = id
        let colors = ["person-blue.png", "person-green.png", "person-orange.png", "person-pink.png", "person-purple.png", "person-red.png", "person-yellow.png"]
        let randomIndex = Int(arc4random_uniform(UInt32(colors.count)))
        var imageName = (colors[randomIndex])
        var cellHeight = TableViewCell.frame.height
        let font = UIFont(name: "Arial Rounded MT Bold", size: 14.0)
        labelHeightConstraint.constant = heightForView(text, font: font!, width: postText.frame.width)
        postImage.clipsToBounds = true
        var timeDouble = time as NSString
        var phpTime = timeDouble.doubleValue
        var currentDate = NSDate()
        var postDate = NSDate(timeIntervalSince1970: phpTime as NSTimeInterval)
        var postTimeOffset = currentDate.offsetFrom(postDate) as String
        /*let url = NSURL(string: "http://bangedabigailtwice.tk/kno/getImage.php?id=\(imageID)")
        var err: NSError?
        var imageData :NSData = NSData(contentsOfURL: url!,options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)!
        var imageFromURL = UIImage(data:imageData)
        */
        self.personImage?.image = UIImage(named: imageName)
        self.postTitle?.text = title
        self.postText?.text = text
        //self.postImage?.image = imageFromURL
        self.postImage.userInteractionEnabled = true
        self.postTime?.text = postTimeOffset 
        self.popularityOutlet?.text = popularity
        let recognizer = UITapGestureRecognizer(target: self, action:Selector("tapImageView:"))
        recognizer.numberOfTapsRequired = 2
        postImage.addGestureRecognizer(recognizer);
    }

If you look at the code above, I have commented out the lines causing the performance issue. Now for my question, how can I temporarily store the image for later use and also how can I dump the temporarily stored images once they have been stored?

Upvotes: 1

Views: 390

Answers (1)

zisoft
zisoft

Reputation: 23078

The lines

mainTableView.estimatedRowHeight = 50.0
mainTableView.rowHeight = UITableViewAutomaticDimension

are called for every row in your table. This is needed only once, so you better move them to viewDidLoad.

No need to downcast the returned cell:

return cell as postWithTitleAndImageTableViewCell

so a

return cell

is sufficient.

For the json part in your code its impossible to see here if there are any performance issues.

Upvotes: 1

Related Questions