My data changes in UITableViewCell when I scroll down and get back

前端 未结 2 1713
天命终不由人
天命终不由人 2021-01-02 18:34

When I\'m populating new data in my tableView Controller, my top cell is being rewrite by the bottom one. Let me explain. I have one image that is loading async in the late

2条回答
  •  天涯浪人
    2021-01-02 19:17

    I think the issue is that while you are scrolling, your images are still in downloading process (that's why you have a nil on that particular line of code). So you have to wait for it.

    I am guessing that you have an array of images. If you don't, then you must make one. You must use it like so:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
            return arrayThatcontainsShopImages.count
    }
    

    Note: that every time you add downloaded image to this array, you should call tableView.reloadData()

    Using SDWebImage can be far superior. Look at the following code using this library, you just create an instance of UIImageView. then using this library, you can download image directy to imageview instance, and to access the image, you can use imageView.image property of UIImageView class

     let imageView = UIImageView()
     imageView.sd_setImageWithURL(someURL, placeholderImage: UIImage())
     self.arrayOfImageViews.append(imageView)
    

提交回复
热议问题