How to load an Image into a Uitableview?

Asked

Viewed 611 times

1

I performed a request that returned me some texts and urls with images, these images I need to load them along with the text in parallel in the table.

I am using Alamofire + Alamofireimage to load the texts/ Images, my doubt and the following, I must perform the process of inserting these elements in the table cell by the method cellForRowAtIndexPath ? why this event is triggered when it is started and when I go through the table, with this the images end up duplicating.

Example:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
    print("Evento acontece")
    if self.verifyUrl(self.texto[indexPath.row]){

        Alamofire.request(.GET, self.texto[indexPath.row])
            .responseImage { response in

                if let image = response.result.value {
                    let size = CGSize(width: 300.00, height: 130.0)
                    let aspectScaledToFillImage = image.af_imageScaledToSize(size)
                    cell.imageView?.image = aspectScaledToFillImage
                }
        }

    }else{

        cell.textLabel?.text = self.texto[indexPath.row]
    }

    return cell
}

inserir a descrição da imagem aqui

Note that besides being poorly aligned there is a text to the right of it, below and loaded the same images.

1 answer

1


Step by step:

I must perform the process of inserting these elements in the cell of the table by method cellForRowAtIndexPath?

Most places where we find codes about UITableView use the method tableView:cellForRowAtIndexPath: to fill in the table data when in fact this method should return a reusable cell as soon as possible.

But where should I fill in my cell data? Answer: In the method tableView:willDisplayCell:forRowAtIndexPath: because as the documentation says this method is called before the cell (which has already been created in the cellForRowAtIndexPath) be drawn and shown on the screen.


I am using Alamofire + Alamofireimage to load the texts/Images

To load the contents of the texts I advise you to use the initial methods of your ViewController using a Array to save the returned content within the closure of the Alamofire.request to fill your array and give a tableView.reloadData() so the data would be ready at the time of formatting the cell.

How much images: using the AlamofireImage you load the images asynchronously using the method af_setImageWithURL(url)

In the end it would be more or less so that it would be:

if self.verifyUrl(self.texto[indexPath.row]){
   cell.imageView?.af_setImageWithURL(NSURL(string: self.texto[indexPath.row])!)
   // outras configurações da imagem....
}else{
   cell.textLabel?.text = self.texto[indexPath.row]
}


Note that besides being poorly aligned there is a text to the right of it, besides below and loaded the same images.

That’s very likely(say 99.99%) is the configuration of your cell in the storyboard specifically your constraints. Try to take a look at this another issue here from pt.stackoverflow 'cause it might help you in that part.

  • In my Viewcontroller has a fun called loadPosts that returns the jsons, these jsons I put in the text array. I will do the tests changing to willdisplaycell, as for the alignment the settings are according to your post, until it was I who asked the question :)

  • Ah is not even had seen , the jsons you speak is the object after the json parse? If you have a drawing of how you want more or less Cell I can help you with the constraints

  • An example of the way I’m loading: https://github.com/Gabrielr47/tableWithImage

  • I’ll take a look and make one pull request afterward.

  • I made the pull request. Take a look because it covers all that I wrote in the reply.

  • I did the pull and to testing/ Adapting to my need, so have ok milestone as accepted worth.

Show 1 more comment

Browser other questions tagged

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