经过多次尝试后,iOS swift tableview.reloadData()无法正常工作

时间:2017-11-30 17:05:23

标签: ios swift uitableview

// **** ****** EDITED

这是我的viewDidLoad方法----->

   override func viewDidLoad() {

    super.viewDidLoad()
    addSlideMenuButton()


    NotificationCenter.default.addObserver(self,
                                           selector: #selector(refreshOrders),
                                           name: NSNotification.Name(rawValue: "refreshedAvailableOrders"),
                                           object: nil)

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(removedAnOrder),
                                           name: NSNotification.Name(rawValue: "removedAvailableOrder"),
                                           object: nil)



    let _ordersManager:OrdersManager = OrdersManager.sharedInstance()


    /* Remove the title so that only a back arrow shows */
    navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    if(_ordersManager.getAvailableLoads() != nil){
        var objects = _ordersManager.getAvailableLoads() as? NSArray


        for (obj) in objects! {
            if let myOrder = obj as? Order{
                var i:Int = 0
                var length:Int = orders.count
                var foundOrder:Bool = false
                while(i<length){

                    if myOrder.OrderLegDistance == nil{
                        myOrder.OrderLegDistance = "--"
                    }

                    if let existingOrder = orders[i] as? order{
                        if(existingOrder.orderId ==  "\(myOrder.OrderId!)"){
                            let loadsDeliveredInt:Int? = Int(myOrder.LoadsDelivered!)
                            let loadCountInt:Int? = Int(myOrder.LoadCount!)
                            var thisLoadsRemaining = loadCountInt! - loadsDeliveredInt!
                            orders[i] = order(dispatchNumber: "\(myOrder.DispatchNumber!)",orderId: "\(myOrder.OrderId!)", source: myOrder.SourceName! , sourceAddress1: myOrder.SourceAddress! , sourceAddress2: myOrder.SourceCity! + ", " + myOrder.SourceState! + " " + myOrder.SourceZip! , destination: myOrder.DestinationName!, destinationAddress1:myOrder.DestinationAddress!, destinationAddress2: myOrder.DestinationCity! + ", " + myOrder.DestinationState! + " " + myOrder.DestinationZip! , loads: "\(myOrder.LoadCount!)", loadsDelivered: "\(myOrder.LoadsDelivered!)",  tons: "\(myOrder.TonsCount!)", price: "\(myOrder.PayRate!)", sourceDistance: myOrder.DistanceToSource!, onewayDistance: myOrder.OrderLegDistance!,  pickupStart: myOrder.PickupBy!, earliestDelivery: myOrder.DeliverStart!,latestDelivery: myOrder.DeliverBy!,product: myOrder.ProductName! , loadsRemaining: "\(thisLoadsRemaining)", truckType: myOrder.TruckType!, notes: myOrder.Notes!, isStarted: myOrder.IsStarted, isOnHold: myOrder.IsOnHold, payRateType: "\(myOrder.PayRateType!)", isStayOn: myOrder.IsStayOn, customerName: myOrder.CustomerName! )

                            foundOrder = true
                        }
                    }
                    i += 1
                }

                if(!foundOrder){


                    if myOrder.OrderLegDistance == nil{
                        myOrder.OrderLegDistance = "--"
                    }

                    let loadsDeliveredInt:Int? = Int(myOrder.LoadsDelivered!)
                    let loadCountInt:Int? = Int(myOrder.LoadCount!)
                    var thisLoadsRemaining = loadCountInt! - loadsDeliveredInt!
                    orders.append(order(dispatchNumber: "\(myOrder.DispatchNumber!)",orderId: "\(myOrder.OrderId!)", source: myOrder.SourceName! , sourceAddress1: myOrder.SourceAddress! , sourceAddress2: myOrder.SourceCity! + ", " + myOrder.SourceState! + " " + myOrder.SourceZip! , destination: myOrder.DestinationName!, destinationAddress1:myOrder.DestinationAddress!, destinationAddress2: myOrder.DestinationCity! + ", " + myOrder.DestinationState! + " " + myOrder.DestinationZip! , loads: "\(myOrder.LoadCount!)", loadsDelivered: "\(myOrder.LoadsDelivered!)",  tons: "\(myOrder.TonsCount!)", price: "\(myOrder.PayRate!)", sourceDistance: myOrder.DistanceToSource!, onewayDistance: myOrder.OrderLegDistance!,  pickupStart: myOrder.PickupBy!, earliestDelivery: myOrder.DeliverStart!,latestDelivery: myOrder.DeliverBy!,product: myOrder.ProductName! , loadsRemaining: "\(thisLoadsRemaining)", truckType: myOrder.TruckType!, notes: myOrder.Notes!, isStarted: myOrder.IsStarted, isOnHold: myOrder.IsOnHold, payRateType: "\(myOrder.PayRateType!)", isStayOn: myOrder.IsStayOn, customerName: myOrder.CustomerName! ))
                }
            }
        }
    }

    if(orders.count == 0){
        noOrdersView.isHidden = false
       // let config = GoVulcanConfig.sharedInstance()
        noOrdersLabel.text = "There Are No Open Orders Available For Pickup Near You"
    }
    else {
        noOrdersView.isHidden = true
    }



}

我在服务器的firebase更新中调用此方法并尝试了所有内容

func refreshOrders(){

    let _ordersManager:OrdersManager = OrdersManager.sharedInstance()

    if(_ordersManager.getAvailableLoads() != nil){
        var objects = _ordersManager.getAvailableLoads() as? NSArray
        if(objects?.count == 0){
            orders.removeAll()
            noOrdersView.isHidden = false

            noOrdersLabel.text = "There Are No Open Orders Available For Pickup Near You"
            DispatchQueue.main.async {
                self.myTableView.reloadData()
            }
        }else{
            noOrdersView.isHidden = true
            DispatchQueue.main.async {
                self.myTableView.reloadData()
            }
        }
        for (obj) in objects! {
            if let myOrder = obj as? Order{
                var i:Int = 0
                var length:Int = orders.count
                var foundOrder:Bool = false
                while(i<length){
                    if myOrder.OrderLegDistance == nil{
                        myOrder.OrderLegDistance = "--"
                    }
                    if let existingOrder = orders[i] as? order{
                        if(existingOrder.orderId ==  "\(myOrder.OrderId!)"){
                            let loadsDeliveredInt:Int? = Int(myOrder.LoadsDelivered!)
                            let loadCountInt:Int? = Int(myOrder.LoadCount!)
                            var thisLoadsRemaining = loadCountInt! - loadsDeliveredInt!
                            orders[i] = order(dispatchNumber: "\(myOrder.DispatchNumber!)",orderId: "\(myOrder.OrderId!)", source: myOrder.SourceName! , sourceAddress1: myOrder.SourceAddress! , sourceAddress2: myOrder.SourceCity! + ", " + myOrder.SourceState! + " " + myOrder.SourceZip! , destination: myOrder.DestinationName!, destinationAddress1:myOrder.DestinationAddress!, destinationAddress2: myOrder.DestinationCity! + ", " + myOrder.DestinationState! + " " + myOrder.DestinationZip! , loads: "\(myOrder.LoadCount!)", loadsDelivered: "\(myOrder.LoadsDelivered!)",  tons: "\(myOrder.TonsCount!)", price: "\(myOrder.PayRate!)", sourceDistance: myOrder.DistanceToSource!, onewayDistance: myOrder.OrderLegDistance!,  pickupStart: myOrder.PickupBy!, earliestDelivery: myOrder.DeliverStart!,latestDelivery: myOrder.DeliverBy!,product: myOrder.ProductName! , loadsRemaining: "\(thisLoadsRemaining)", truckType: myOrder.TruckType!, notes: myOrder.Notes!, isStarted: myOrder.IsStarted, isOnHold: myOrder.IsOnHold, payRateType: "\(myOrder.PayRateType!)", isStayOn: myOrder.IsStayOn, customerName: myOrder.CustomerName! )
                            foundOrder = true
                        }
                    }
                    i += 1
                }

                if(!foundOrder){
                    if myOrder.OrderLegDistance == nil{
                        myOrder.OrderLegDistance = "--"
                    }
                    let loadsDeliveredInt:Int? = Int(myOrder.LoadsDelivered!)
                    let loadCountInt:Int? = Int(myOrder.LoadCount!)
                    var thisLoadsRemaining = loadCountInt! - loadsDeliveredInt!
                    orders.append(order(dispatchNumber: "\(myOrder.DispatchNumber!)",orderId: "\(myOrder.OrderId!)", source: myOrder.SourceName! , sourceAddress1: myOrder.SourceAddress! , sourceAddress2: myOrder.SourceCity! + ", " + myOrder.SourceState! + " " + myOrder.SourceZip! , destination: myOrder.DestinationName!, destinationAddress1:myOrder.DestinationAddress!, destinationAddress2: myOrder.DestinationCity! + ", " + myOrder.DestinationState! + " " + myOrder.DestinationZip! , loads: "\(myOrder.LoadCount!)", loadsDelivered: "\(myOrder.LoadsDelivered!)",  tons: "\(myOrder.TonsCount!)", price: "\(myOrder.PayRate!)", sourceDistance: myOrder.DistanceToSource!, onewayDistance: myOrder.OrderLegDistance!,  pickupStart: myOrder.PickupBy!, earliestDelivery: myOrder.DeliverStart!,latestDelivery: myOrder.DeliverBy!,product: myOrder.ProductName! , loadsRemaining: "\(thisLoadsRemaining)", truckType: myOrder.TruckType!, notes: myOrder.Notes!, isStarted: myOrder.IsStarted, isOnHold: myOrder.IsOnHold, payRateType: "\(myOrder.PayRateType!)", isStayOn: myOrder.IsStayOn, customerName: myOrder.CustomerName! ))
                }
            }
        }
    }

这就是我填充单元格的方式

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

      //use orders array to populate table cell

}

我正在调试其他人的代码并且它实施得非常糟糕。代码基本上构建了一个orders数组。

我正在尝试刷新我的tableview,但它无法正常工作。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我发现代码存在很多问题,但有些内容可能与您的刷新问题有关: (我假设numberOfRowsInSection和cellForRowAt使用orders属性)

  • 您是否可以通过打印输出或断点检查在填充订单数组后调用numberOfRowsInSection和cellForRowAt?
  • 您能检查表视图数据源是否设置为当前类?
  • 如果其他任何事情失败,我建议您尝试在tableView中设置一些硬编码值,只是为了看它是否作为UI组件工作,然后修复订单管理并确保重新加载数据是数组可用后调用。
祝你好运:)