简体   繁体   English

在表格视图中滚动时图像重复单元格

[英]images repeating cells when scrolled in tableview

images are not showing properly in tableview , I have two Json Api (Primary/high) school.图像在 tableview 中显示不正确,我有两个 Json Api(小学/高中)学校。 I can append the Both Json api Data and display into tableview, tableview working fine it's showing both(primary/high) school data.我可以将两个 Json api 数据附加到 tableview 中,tableview 工作正常,它显示两个(小学/高中)学校数据。 when I can scroll the tableview images are jumping and images loading very slow in image view at tableview.当我可以滚动 tableview 时,图像正在跳跃,并且图像在 tableview 的图像视图中加载速度非常慢。

Before scrolling tableview its showing like this在滚动 tableview 之前它的显示是这样的

在此处输入图片说明

在此处输入图片说明

After scrolling the tableview it's shows like this滚动 tableview 后,它显示如下

after scrolling images are jumping,滚动图像跳跃后,

在此处输入图片说明

this is the code这是代码

     var kidsdata = [KidDetails]()

    func getprimarydata(_firsturl: String,firstid:String,updatedate:String)
    {


                                        if errorCode == "0" {

                                      if let kid_list = jsonData["students"] as? NSArray {

                                      self.kidsdata.removeAll()

                                      for i in 0 ..< kid_list.count {

                                      if let kid = kid_list[i] as? NSDictionary {


                                      let imageURL = url+"/images/" + String(describing: kid["photo"]!)

                                      self.kidsdata.append(KidDetails(
                                                            name:kid["name"] as? String,
                                                            photo : (imageURL),
                                                            standard: ((kid["standard"] as? String)! +  "std" + " " + (kid["section"] as? String)! + " section ")
                                                            ))}}}}
 }


      func gethighdata(_secondurl:String ,secondid:String,updatedate:String)
        {
         if errorCode == "0" {

                                if let kid_list = jsonData["students"] as? NSArray {
                                    for i in 0 ..< kid_list.count {


                                        if let kid = kid_list[i] as? NSDictionary {

                                            let imageURL = url+"/images/" + String(describing: kid["photo"]!)

                                            self.kidsdata.append(KidDetails(
                                                name:kid["name"] as? String,
                                                photo : (imageURL),

                                    standard: ((kid["standard"] as? String)! + "th" + " " + (kid["section"] as? String)! + " section ")
                                                )
                                            )
                                        }
                                    }


                                self.do_table_refresh()
                            }
                            }
    }


        func do_table_refresh()
        {

            DispatchQueue.main.async(execute: {

                self.TableView.reloadData()

                return
            })

        }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell =
                tableView.dequeueReusableCell(
                    withIdentifier: "cell", for: indexPath) as! DataTableViewCell
            cell.selectionStyle = .none

            cell.ProfileImage?.image = nil

            let row = (indexPath as NSIndexPath).row


            let kid = kidsdata[row] as KidDetails

            cell.NameLabel.text = kid.name

            cell.ProfileImage.image = UIImage(named: "profile_pic")

            cell.ProfileImage.downloadImageFrom(link:kid.photo!, contentMode: UIViewContentMode.scaleAspectFill)
            cell.ClassNameLabel.text = kid.standard
           return cell
        }

where I did mistake pls help me....!我做错的地方请帮助我....!

AlamofireImage handles this very well. AlamofireImage处理得很好。 https://github.com/Alamofire/AlamofireImage https://github.com/Alamofire/AlamofireImage

import AlamofireImage

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DataTableViewCell
                cell.selectionStyle = .none

                let kid = kidsdata[indexPath.row] as KidDetails


                cell.NameLabel.text = kid.name
                cell.ClassNameLabel.text = kid.standard

                // assuming cell.ProfileImage is a UIImageView
                cell.ProfileImage.image = nil

                let frame = CGSize(width: 50, height: 50)
                let filter = AspectScaledToFillSizeWithRoundedCornersFilter(size: frame, radius: 5.0)

                cell.ProfileImage.af_setImage(withURL: urlToImage, placeholderImage: nil, filter: filter,
                                      imageTransition: .crossDissolve(0.3), runImageTransitionIfCached: false)

               return cell
    }

All we need to do is use the prepareForReuse() function.我们需要做的就是使用prepareForReuse()函数。 As discussed in this medium article, This function is called before cell reuse, letting you cancel current requests and perform a reset.正如这篇中篇文章中所讨论的,在单元重用之前调用此函数,让您取消当前请求并执行重置。

override func prepareForReuse() {
    super.prepareForReuse()

    ProfileImage.image = nil
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM