Request for slow scrolling json for tableview

Asked

Viewed 99 times

1

I have an application that returns a json to a tableview, but each time I roll the cells it kind of "chokes", I was told that I would have to leave the return function of the asynchronous json... Someone knows how to handle it on Swift?

There are only 10 lines with image and next to a label, but with tests done, Ih that the problem of slowness is only with the loading of the images that has in the cells in the cellForRowAtIndexPath method if I comment from the line that picks the image url until the end is good, It is as if the Cellforatindexpath method makes every image request at every scroll.

The part of the code you have pro...

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell:Cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! Cell


        cell.info.text = arrayJson[indexPath.row]
        cell.info.font = UIFont.systemFontOfSize(12.0)

        if let url = NSURL(string: self.arrayJsonImages[indexPath.row]) {

             let data = NSData(contentsOfURL: url)

                cell.imagem.contentMode = UIViewContentMode.ScaleAspectFit
                cell.imagem.image = UIImage(data: data!)
                cell.imagem.layer.masksToBounds = true;
                cell.imagem.layer.cornerRadius = 15;

        }


            return cell


    }
  • Are you populating with a lot of data? Because we usually create a "limitation". Tavelz is this, but details are missing to be sure of your problem.

  • I’ve already edited the question, I think it’s clearer now...

  • I’m sorry friend but it’s still hard to understand what "you did" exactly, I recommend you read this: http://answall.com/help/mcve. - I’m sure you’ll take this as constructive criticism :)

1 answer

2


In fact you will need to asynchronously download this image so that it does not give these locked in your table.

Start with something like this:

if let imageUrl = NSURL(string: arrayJsonImages[indexPath.row]) {
    let request = NSURLRequest(URL: imageUrl)
    let requestQueue : NSOperationQueue = NSOperationQueue()

    NSURLConnection.sendAsynchronousRequest(request, queue: requestQueue, completionHandler: { (response, responseData, error) -> Void in
        if error == nil {
            cell.image = UIImage(data: responseData!)
        }
    })
}

This already solves your locking problem. Now, to optimize further, it would be interesting to store these images in a directory of cache, so it will not be necessary to download every time if this is your case.

If you want something more robust, a "placeholder", the very management of cache and etc, you can search for something already ready as for example the Sdwebimage, which is a Category of UIImageView.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.