TableView在显示两个嵌套的alamofire请求之前不会显示数据

时间:2018-11-01 19:26:35

标签: swift uitableview alamofire swifty-json

我有两项服务可为我提供数据,第一个服务可获取经度和纬度。我将它们发送给Google定位服务,让我获得“ formatted_address”,然后将此格式化的地址填充到TableView中

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<select name="Deliveryaddress.Country" id="Deliveryaddress.Countryctl01_ctl26_ctl00_ctl00_ctl05_ctl01_ctl00_ctl04_ctl00_ctl02_ctl00_Deliveryaddress.Country" class="form-control">
  <option selected="selected" value="+420">Česká republika</option>
  <option value="+421">Slovenská republika</option>
</select>



<input name="deliveryaddress.Phone" type="text" id="deliveryaddress.Phone" class="form-control required">

获取位置的经度和纬度 而对于Google api而言:

function_name = {'key1':['apple', 'orange'], 'key2':['orange', 'pear']}
foobar = {}
for key, value in function_name.items():
    for element in value:
        if element in foobar:
            foobar[element] += 1
        else:
            foobar[element] = 1
print(foobar)

最后,在cellForRowAt

func getData(){
    locDataArray.removeAll()

     let url = "http://someurl/Report/v1/TripReport?UserID=101&Username=ewe2020&DeviceID=2647&FromDate=25/10/2018%2006:00%20AM&lang=ar&ToDate=25/10/2018%2009:00%20PM"

        // or if you need the string
        print(url)

        Alamofire.request(url).responseJSON { (responseData) -> Void in
            if((responseData.result.value) != nil) {
                let swiftyJsonVar = JSON(responseData.result.value!)

                if let data = swiftyJsonVar.stringValue.data(using: .utf8) {
                    if let json = try? JSON(data: data) {
                        for item in json["data"].arrayValue {
                            self.locDataArray.append(LocData(fromJson: item))
                        }
                    }

                    DispatchQueue.main.async{
                        self.getAddress()
                        self.table.reloadData()
                    }
                }
            }
        }
}

仅在向上或向下滚动后显示数据。

1 个答案:

答案 0 :(得分:0)

if((responseData.result.value) != nil) {
                let swiftyJsonVar = JSON(responseData.result.value!)

                self.locNameFrom.append((swiftyJsonVar.dictionary!["results"]?[0]["formatted_address"].string)!)
                self.table.reloadData() // <-- here you should reloadData.
            }