2017-03-28 1 views
-2

테이블보기가 맨 아래에 도달하면 SQL Server에서 반환 된 더 많은 결과를로드하는 메서드를 구현하고 있습니다.Swift tableview 더 많은 데이터로드

내가 직면 한 문제는 getData() 메서드가 루프에서 호출되어 그 이유를 파악할 수 없다는 것입니다. 응용 프로그램이 종료 될 때까지

로그 출력 아래 반복 :

인덱스 경로 : 12
마지막 요소 : 12
인덱스 경로 : 12
마지막 요소 : 12

나는 루프를 만드는 그 중 하나를 의심하지만 변경해야할 것을 해결할 수는 없다.

이 가진 모든 도움을 많이

을 감사이 내 willDisplayCell 방법입니다 :

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
     let lastElement = id.count - 1 
     if indexPath.row == lastElement { 

      print("Index Path:\(indexPath.row)") 
      print("Last Element: \(lastElement)") 
       limitf = "\(lastElement)" 
       getData() 
     } 
    } 

내가 데이터를 얻기 위해 사용하고있는 기능 :

func getData(){ 

     let defaults = UserDefaults.standard 
     let userID = defaults.string(forKey: "id") 

     if(limitf == ""){ 
     id.removeAll() 
     firstname.removeAll() 
     lastname.removeAll() 
     fbid.removeAll() 
     image.removeAll() 
     totalratings.removeAll() 
     mobile.removeAll() 
     } 

     let url = NSURL(string: "https://www.****/getapplications.php?&jobid=\(jobid)&limitf=\(limitf)") 
     // print(url!) 
     let task = URLSession.shared.dataTask(with: url as! URL) { (data, response, error) -> Void in 
      if let urlContent = data { 
       do { 
        if let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: []) as? [[String:AnyObject]] { 
         var i = 0 
         while i < jsonResult.count { 

          self.id.append(jsonResult[i]["id"]! as! String) 
          self.firstname.append(jsonResult[i]["firstname"]! as! String) 
          self.lastname.append(jsonResult[i]["lastname"]! as! String) 
          self.fbid.append(jsonResult[i]["fbid"]! as! String) 
          self.image.append(jsonResult[i]["image"]! as! String) 
          self.totalratings.append(jsonResult[i]["totalratings"]! as! String) 
          self.mobile.append(jsonResult[i]["mobile"]! as! String) 

          i = i + 1 
         } 
        } 
       } catch { 
        print("JSON serialization failed") 
       } 
      } else { 
       print("ERROR FOUND HERE") 
      } 

      DispatchQueue.main.async { 
       self.tableView.reloadData() 
      } 

     } 

     task.resume() 



    } 
} 
+0

플래그가 프로세스가 이미 실행 중임을 나타내는 경우 단순히 반환하도록 'getData'가 실행 중일 때 검색 할 플래그를 추가하려고 시도 했습니까? 이런 식으로,'getData'가 연속적으로 두 번 호출된다면, 그것은 한 번만 실행될 것입니다. – Fahim

답변

1

서버에 12 개의 레코드 만 있고 12 번째 레코드가 화면에 표시되는 경우 표시 될 때마다 getData()가 호출됩니다. 서버는 더 이상 레코드를 반환하지 않으므로 (카운트는 12로 남음) .reloadData()를 호출하면 12 개의 레코드가 다시 표시되고 12 번째에 getData()가 다시 호출됩니다.

새 레코드를받지 못한 경우 reloadData()를 호출하면 안됩니다.

0

을 willDisplayCell에서 마지막 셀을 표시하기 위해 getData()를 호출하고 있습니다. 그런 다음 내용을 지우고 표를 다시로드하십시오. 즉, 마지막 셀이 항상 데이터를 다시로드하기 위해 호출되므로 루프가 계속 유지됩니다.

마지막 행이로드 될 때 수행중인 작업을 감지해야하지만 기존 데이터를 지우지 않고 getData()를 호출하면 새 데이터 행을 추가하고 insertRowsAtIndexPaths를 사용하여 tableview.

0

getData()에서 self.tableView.reloadData()를로드하는 데 문제가 있습니다. 이 메서드는 다시 한 번 해당 메서드를 tableView 대리자에서 실행할 것을 의미합니다. self.tableView.reloadData()를 멀리두면이 루프가 종료됩니다.

관련 문제