在tableview中滚动时重复单元格的图像

时间:2017-09-01 21:12:07

标签: ios arrays swift uitableview

图像在tableview中没有正确显示,我有两个Json Api(小学/高中)学校。我可以将两个Json api数据和显示附加到tableview中, tableview正常工作,显示(主要/高)学校数据。当我可以滚动桌面视图时,图像在桌面视图中跳跃并且图像加载速度非常慢。

在滚动tableview之前,它会像这样显示

enter image description here

enter image description here

滚动桌面视图后,它会显示如下

滚动图像后跳转,

enter image description here

这是代码

     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
        }

我错误的地方请帮助我....!

1 个答案:

答案 0 :(得分:0)

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
    }