2017-09-01 126 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

相關問題