2017-03-10 9 views
0

I持つ行を持つテーブルを作成するため、次のTableViewControllerコード:のUITableView注文ペン先行

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    switch (arrayData[indexPath.row].cell) { 
    case 0: 

     let cell = Bundle.main.loadNibNamed("FirstCell", owner: self, options: nil)?.first as! FirstCell 
     if arrayData[indexPath.row].image == nil { 
      cell.firstImage.image = arrayData[indexPath.row].image 
     } else { 
      cell.firstImage.image = #imageLiteral(resourceName: "pump") 
     } 
     cell.firstCellLabel.text = arrayData[indexPath.row].text 
     cell.backgroundColor = UIColor.clear 

     return cell 

    case 1...98: 
     let cell = Bundle.main.loadNibNamed("StationPumpCell", owner: self, options: nil)?.first as! StationPumpCell 
     //cell.pumpImage.image = arrayData[indexPath.row].image 
     cell.pumpLabel.text = arrayData[indexPath.row].text 
     cell.backgroundColor = UIColor.clear 

     switch (arrayData[indexPath.row].stat) { 
      case 1: 
       cell.pumpImage.image = #imageLiteral(resourceName: "online") 
      case 3: 
       cell.pumpImage.image = #imageLiteral(resourceName: "warning") 
       cell.pumpLabel.textColor = UIColor.orange 
      default: 
       cell.pumpImage.image = #imageLiteral(resourceName: "offline") 
       cell.pumpLabel.textColor = UIColor.lightGray 
     } 

     if (arrayData[indexPath.row].map == 0) { 
      cell.pumpLabel.text?.append(" - test") 
     } 

     return cell 

    case 99: 
     let cell = Bundle.main.loadNibNamed("LastCell", owner: self, options: nil)?.first as! LastCell 
     return cell 

    default: 
     let cell = Bundle.main.loadNibNamed("FirstCell", owner: self, options: nil)?.first as! FirstCell 
     //cell.firstImage.image = arrayData[indexPath.row].image 
     //cell.firstCellLabel.text = arrayData[indexPath.row].text 

     print("oops") 

     return cell 
    } 

} 

配列は、これが

cellData(cell : 0, text : self.annotationTitle, image: self.stationImage, stat: nil, map: nil) 

上記「0」ヘッダであるべきであり、常にのように見えます最初にしてください。 次のセルには1〜98の番号が付けられ、フッターは99になります。つまり、順序は、ヘッダー、データの行、フッターです。

問題は、フッターが一番上にあることがあります。オーダーされたフッター、ヘッダー、データ。他の時間はそのフッター、データ、ヘッダー。私はまだ正しい順序を得ることができませんでした。

これは、オンラインjsonファイルから収集されたデータと、内部にコンテンツがないフッターより少し遅れて読み込まれるデータとは関係があるかどうかわかりません。 Stationimageのデバッグ出力は負荷が、デバッグウィンドウとヘッダーセルに係るない画像を示さないことが非常に最後のものであることを

Alamofire.request(stationsURL).responseJSON { response in 

     if response.result.value != nil { 
      NSLog("Success") 
      let stationsJSON = JSON(response.result.value!) 

      let stationResp = stationsJSON["Stations"][0]["photos"][0].stringValue 

      stationPhoto = stationResp 

      print(stationPhoto) 

      let imageURL = "http://www.123.com/files/thumb100_" + stationPhoto 

      print(imageURL) 

      Alamofire.request(imageURL).responseImage { response in 

       if response.result.value != nil { 

        self.stationImage = response.result.value! 

        debugPrint("Stationimage: ") 
        debugPrint(self.stationImage) 

       } 
      } 

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

ヘッダ(セル0)用の画像は、このように検出されました:"Stationimage: " <UIImage: 0x17028ffa0>, {50, 37.5}と私はこれが悪い順序を引き起こしているかもしれないと思う。

2017-03-10 04:34:00.290406 Testfile[14214:6238055] [INFO] {}[Database]: recovered 12 frames from WAL file …Testfile/Cache.db-wal (Code 283) 
2017-03-10 04:34:00.678731 Testfile[14214:6237998] Success 
Demo 
http://**json address** 
2017-03-10 04:34:07.753937 Testfile[14214:6237998] Success 
p9_2.jpg 
http://**json address**/thumb100_p9_2.jpg 
[Testfile.cellData(cell: 99, text: nil, image: nil, stat: nil, map: nil), 
Testfile.cellData(cell: 0, text: Demo, image: Optional(<UIImage: 0x17409d1a0>, {0, 0}), stat: nil, map: nil)] 
2017-03-10 04:34:07.872660 Testfile[14214:6237998] Success 
5 
[Testfile.cellData(cell: 99, text: nil, image: nil, stat: nil, map: nil), 
Testfile.cellData(cell: 0, text: Demo, image: Optional(<UIImage: 0x17409d1a0>, {0, 0}), stat: nil, map: nil), 
Testfile.cellData(cell: 1, text: Name 1, image: nil, stat: 1, map: 1), 
Testfile.cellData(cell: 2, text: Name 2, image: nil, stat: 1, map: 1), 
Testfile.cellData(cell: 3, text: Name 3, image: nil, stat: 1, map: 1), 
Testfile.cellData(cell: 4, text: Name 4, image: nil, stat: 0, map: 1), 
Testfile.cellData(cell: 5, text: Name 5, image: nil, stat: 0, map: 1)] 
"Stationimage: " 
<UIImage: 0x17409f8b0>, {50, 37.5} 

0を常に最初に、99を最後にするようにセルを順序付ける正しい方法は何ですか?

答えて

0

バックエンドのクエリ(応答をソートする)から、またはコードによって変更します。あなたがコードで欲しいなら、いくつかのコードを表示してください。

+1

ありがとう、ファリッド。私は 'self.arrayData.sort {$ 0.cell <$ 1.cell} 'でこれを部​​分的に解決しましたが、画像もロードされています。これは常にセルを正しくソートしますが、これは素晴らしいことですが、別の問題があります。セル0には画像があるので、読み込むには数ミリ秒以上かかる。モーダルが開かれると、セル1〜99はしばしば(必ずしもそうではない)高速でロードされ、画面の上部に揃えられ、0がロードされます。クールなことは、セル1〜99がセル0のための部屋を作るとき、画面上にわずかなジャンプがあるということです。これをどのように動作させるか分かりません。私が 'sleep(1)'をコードに入れた場合、それはうまく読み込まれますが、大きな解決策ではありません。 – Fid

関連する問題