UITableView订购nib行

时间:2017-03-10 01:38:33

标签: ios json swift uitableview

我有以下TableViewController代码用于创建包含行的表:

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: " <UIImage: 0x17028ffa0>, {50, 37.5},调试打印的Stationimage是最后一个加载的东西,我认为这可能是造成错误排序的原因。

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始终是最后一个?

1 个答案:

答案 0 :(得分:0)

要么是从后端的查询(对响应进行排序)还是通过代码进行更改。如果您想通过代码,请显示一些代码。