2017-09-01 101 views
0

图像在tableview中没有正确显示,我有两个Json Api(初级/高级)学校。我可以将两个Json API数据和显示添加到tableview中, tableview工作正常,它显示两个(主/高)学校数据。当我可以滚动tableview图像跳跃和图像加载非常缓慢的图像视图在tableview。图像在桌面视图中滚动时重复单元格

之前滚动的tableview其显示这样

enter image description here

enter image description here

滚动它显示像滚动的图片后,该

都跳的tableview后,

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 
     } 

,我没有错误请帮助我....!

+0

在您的tableViewCell类中,您需要重置函数'prepareForReuse'中的图像。这将在每次细胞被重复使用时被调用,并且是可以移除图像的最难点。该函数在UITableViewCell中实现,你必须覆盖它 –

+0

啊,你下载一个图像,这个函数是在后台线程中运行下载并将图像放在主线程中? –

+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 
    } 
+0

上午在应用程序启动第一次应用程序时使用tablebar在标签栏控制器tableview不滚动和无法访问tabbar项目,重新运行应用程序后它工作正常,如何结束com这个问题 – naga

+0

tableview滚动工作后,我的项目干净,并重新运行应用 – naga

+0

我试图添加AlamofireImage应用程序崩溃 – naga

相关问题