アプリを起動するとサーバからデータを引っ張ってきてtableviewに表示させようとしているのですが、アプリが起動してもしばらくtableviewにデータが表示されず、10秒くらい経つとやがてデータが表示されます。
10秒くらい経つとcellForRowAtが呼ばれることを確認しました。
しかしアプリを起動した後すぐにtableviewをスクロールするとデータが表示されます。
つまり、アプリを起動して放っておくと時間差でデータが表示されるのですが、アプリを起動してtableviewをスクロールするとデータがtableviewに表示されます。
どうしてこんなことが起こるのか全くわかりません。
アプリが起動したらすぐにtableviewにそのままデータが表示されるようにしたいです。
下記にtableviewとapi通信周りのコードを記します。
どなたかわかる方がいれば教えていただきたいです。よろしくお願いします。
swift
1ViewController 2 3 4override func viewDidLoad() { 5 super.viewDidLoad() 6 7 self.getHitokotoFromServer() 8} 9 10func getHitokotoFromServer() { 11 HitokotoManager.sharedInstance.hitokots.removeAll() 12 13 let myUrl = URL(string: "http://localhost/hitokoto/getAll.php") 14 if let url = myUrl { 15 HitokotoAPIClient.sharedInstance.requestHitokotoGet(url: url, completion: {(result: Result) in 16 switch result { 17 case .success(let json): 18 json.forEach({ 19 let hitokoto = Hitokoto() 20 hitokoto.setHitokoto(hitokoto: $0) 21 HitokotoManager.sharedInstance.hitokots.append(hitokoto) 22 }) 23 HitokotoManager.sharedInstance.hitokots.sort(by: {$0.date.compare($1.date) == .orderedDescending}) 24 self.tableView.reloadData() 25 26 case .failure: 27 break 28 } 29 }) 30 } 31 } 32 33func setupTableView() { 34 self.tableView.estimatedRowHeight = 44 35 self.tableView.rowHeight = UITableViewAutomaticDimension 36 } 37 38// MARK: - UITableViewDataSource 39 40 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 41 if self.searchController.isActive { 42 return self.searchResults.count 43 } else { 44 return HitokotoManager.sharedInstance.hitokots.count 45 } 46 } 47 48 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 49 if self.isShowingDetailCell { 50 let cell = tableView.dequeueReusableCell(withIdentifier: "HitokotoTableViewDetailCell", for: indexPath) as! HitokotoDetailTableViewCell 51 if self.searchController.isActive { 52 cell.fillWith(hitokoto: self.searchResults[indexPath.row]) 53 } else { 54 cell.fillWith(hitokoto: HitokotoManager.sharedInstance.hitokots[indexPath.row]) 55 } 56 57 return cell 58 } else { 59 let cell = tableView.dequeueReusableCell(withIdentifier: "HitokotoTableViewCell", for: indexPath) as! HitokotoTableViewCell 60 if self.searchController.isActive { 61 cell.fillWith(hitokoto: self.searchResults[indexPath.row]) 62 } else { 63 cell.fillWith(hitokoto: HitokotoManager.sharedInstance.hitokots[indexPath.row]) 64 } 65 66 return cell 67 } 68 } 69
Swift
1HitokotoAPIClient 2 3 4func requestHitokotoGet(url: URL, completion: @escaping (Result) -> Void) { 5 var request = URLRequest(url: url) 6 request.httpMethod = "GET" 7 URLSession.shared.dataTask(with: request, completionHandler: {(data, response, error) in 8 if error == nil && data != nil { 9 let jsonObject = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [[String: Any?]] 10 if let json = jsonObject?.flatMap({$0}) { 11 completion(Result.success(json: json)) 12 } 13 } else { 14 completion(Result.failure) 15 } 16 }).resume() 17 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/26 12:50