2017-03-10 42 views
0

我有用於創建與行的表中的下列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文件中收集的數據有關,以及某些單元比頁腳稍晚加載,而頁腳沒有內容。

頁眉(小區0)的圖像被發現是這樣的:

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() 
     } 
    } 

這Stationimage的調試打印是最後一件事,負載,根據調試窗口,標題單元格顯示沒有圖像:"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具有圖片,因此加載需要幾個ms。打開模式後,單元格1-99通常(不總是)加載速度更快,並對齊屏幕頂部,然後加載0。怪異的是,當單元格1-99爲單元格0騰出空間時,屏幕上會有輕微的跳躍。不知道如何解決這個問題。如果我在代碼中加入'sleep(1)',它會很好地加載,但不是一個好的解決方案。 – Fid