2017-03-10 167 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