2017-08-09 53 views
0

我目前有一個Xcode項目,可以加載數據並將它們放入數組中,該數組將被排序並放置在一張桌子上。截至目前,當我進入viewDidLoad()部分的表格時,我有一個函數可以收集我需要的所有數據。但是爲了阻止應用程序崩潰,它加載了30個限制。我目前的問題是,一旦用戶滾動瀏覽了30個項目,我想在最初的30個(標準滾動)下面加載30個項目。我可以考慮的唯一方法是重新加載整個表,這不是很聰明。像Instagram或任何應用程序一樣向下滾動時加載

我只想要一個標準的滾動功能,您可以在Instagram,Facebook或在桌面視圖中加載數據的任何互聯網應用中看到這些功能。所以當用戶滾動時,更多的數據被添加到底部。下面我抄我的代碼我用來收集原始數據:

func findAnimalUsers() { 
     //STEP 1: Find users 
       let animalQuery = PFQuery(className: "Animals") //choosing class 
       animalQuery.whereKey("dog", equalTo: animalType.text!) //getting users with animal type user types 
       animalQuery.limit = 30 //number of users intitally showing 
       animalQuery.findObjectsInBackground (block: { (objects, error) -> Void in 
        if error == nil { //if no error 

         //clean up 
         self.animalArray.removeAll(keepingCapacity: false) 

         //STEP 2: Find related objects depending on query setting 
         for object in objects! { 
          self.animalArray.append(object.value(forKey: "user") as! String) //objectId of related users 
         } 

         //STEP 3: Find users 
         let query = PFUser.query() 
         query?.whereKey("objectId", containedIn: self.animalArray) //finding users 
         query?.addDescendingOrder("createdAt") //how to order users 
         query?.findObjectsInBackground(block: { (objects, error) -> Void in 
          if error == nil { 

           //clean up 
           self.usernameArray.removeAll(keepingCapacity: false)             

           self.profilePhotoArray.removeAll(keepingCapacity: false) 
           self.objectIDArray.removeAll(keepingCapacity: false) 

           //find related objects depending on query setting 
           for object in objects! { 
            self.usernameArray.append(object.object(forKey: "username") as! String) 
            self.profilePhotoArray.append(object.object(forKey: "profilePhoto") as! PFFile) 
            self.objectIDArray.append(object.objectId!) 

           } 
          } else { 
           print(error) 
          } 
         }) 
        } else { 
         print(error) 
        } 
       }) 
      } 

我還添加了的表如何使用這些信息的代碼:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     //define cell 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! AnimalsCell 

     //STEP 1: connect data from server to objects 
     cell.usernameLabel.text = usernameArray[indexPath.row] 
     cell.objectID = objectIDArray[indexPath.row] 
     profilePhotoArray[indexPath.row].getDataInBackground (block: { (data, error) in 
      if error == nil { 
       cell.profilePhoto.image = UIImage(data: data!)     
      } else { 
       print(error) 
      }    
     }) 

所以,當這個視圖控制器來了在viewDidLoad()中,我只有findAnimalUsers()來啓動初始加載。 如何在滾動時加載更多?

+0

看看https://github.com/pronebird/ UIScrollView-InfiniteScroll –

回答

1

委託方法可以使用滾動視圖的委託方法

func scrollViewDidScroll(_ scrollView: UIScrollView) 
{ 
    let offsetY = scrollView.contentOffset.y 
    let contentHeight = scrollView.contentSize.height 
    let scrollHeight = scrollView.frame.size.height 
    if offsetY >= contentHeight - scrollHeight 
    { 
     //your logic 
    } 
} 
+0

我試着用這個和我的邏輯我通過數組中的項數來決定應該在表中有多少行,從而跳過查詢。但是當我開始滾動時,它會加載多個重複項。 'animalQuery.skip = objectIDArray.count'我該如何解決這個問題? – fphelp

+0

@fphelp在你的情況下,objectId應該是唯一的,所以當你滾動你的最後一個objectId將是你第一次獲取數據的下一個30;就像如果你現在有最後一個objectId是30,那麼在你的下一個滾動中,你將從objectId 31加載數據,然後你將在數組中追加數據並在追加數據後重新加載表,以便你不會得到重複的數據。 –

+0

嗯。我不確定我是否明白你完全想要說什麼。爲了得到表中所需的行數,它計算數組object'Array.count中的對象。 **我是否應該沒有用相同的數組跳過相同的數組來填充行數?** – fphelp

1

您可以輕鬆地做到這一點與tableView

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell , forRowAtIndexPath indexPath: NSIndexPath) { 

    if indexPath.row == lastElement { 
     // handle your logic here to get more items, add it to dataSource and reload tableview 
    } 
} 
+0

我試圖實現這一點,但構建失敗,它突出顯示'lastElement'的錯誤說:「使用未解析的標識符'lastElement'」我該如何解決這個問題? – fphelp

+0

lastElement僅僅是一個參考,你必須首先在你的代碼中定義它,並將它設置爲你項目列表中的最後一個indexpath。 –

+0

簡而言之,lastElement = <你的表格數據> .count,你必須在你的班級中全局定義。 –

相關問題