// **** ****** 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,但它无法正常工作。有什么想法吗?
答案 0 :(得分:1)
我发现代码存在很多问题,但有些内容可能与您的刷新问题有关: (我假设numberOfRowsInSection和cellForRowAt使用orders属性)