【问题标题】:UITableView ordering nib rowsUITableView 排序笔尖行
【发布时间】:2017-03-10 01:38:32
【问题描述】:

我有以下 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 的调试打印是最后加载的内容,根据调试窗口,标题单元格没有显示图像:"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 始终在后的正确方法是什么?

【问题讨论】:

    标签: ios json swift uitableview


    【解决方案1】:

    您可以从后端的查询(您对响应进行排序)或通过代码更改它。如果你想通过代码,请显示一些代码。

    【讨论】:

    • 谢谢,法里德。我已经用self.arrayData.sort { $0.cell &lt; $1.cell } 部分解决了这个问题,并且图像现在正在加载。这总是正确地对单元格进行分类,这很好,但还有另一个问题。由于单元格 0 有图片,因此加载需要几毫秒。打开模式时,单元格 1-99 通常(并非总是)加载速度更快并与屏幕顶部对齐,然后加载 0。奇怪的是,当单元格 1-99 为单元格 0 腾出空间时,屏幕上会出现轻微的跳跃。不知道如何解决这个问题。如果我将sleep(1) 放入代码中,它会很好地加载,但不是一个很好的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多