簡體   English   中英

在主線程上重新加載UITableView並不總是有效

[英]Reloading UITableView on Main Thread does not always work

我有一個問題,當我在主隊列上重新加載UITableView的數據時,它不會一直重新加載數據。 當我啟動應用程序並重新加載一些條紋(3到5次啟動),然后一些不重載(2到3次啟動)時,它只會大約在5次中重新加載數據。

我正在使用Swift 2在iOS 6和Xcode 7 Beta 4的iPhone 6上測試這個應用程序。

這是我的UIViewController持有UITableView的代碼,它也是啟動后應用程序的初始屏幕。

import UIKit

class TableController: UIViewController, CLLocationManagerDelegate {
    let rowTitles = ["Date", "Temp", "Humidity", "Wind"]
    var tableData = [String : String]()

    override func viewDidLoad() {
        self.setupLocationManager()
    }

    func setupLocationManager() {

        // Setting up a location manager 

        self.locationManager.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        LocationData.ownLocation = locations.last!
        self.locationManager.stopUpdatingLocation()

        self.setupTable()
    }

    func setupTable() {
        ApiClient.getCurrentWeatherAndSunData(LocationData.ownLocation) { (data, error) -> Void in
            guard error == nil else {
                print("Could not get current weather and sun data")
                return
            }

            WeatherData.main = data!.valueForKey("main") as! Dictionary<String, AnyObject>
            WeatherData.wind = data!.valueForKey("wind") as! Dictionary<String, AnyObject>

            self.setWeatherData()

            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.ownTableView.reloadData()
            })
        }
    }

    func setWeatherData() {
        let temp = String(WeatherData.main["temp"] as! Int)
        let humidity = String(WeatherData.main["humidity"] as! Int)
        let windSpeed = String(WeatherData.wind["speed"] as! Int)

        self.tableData["Date"] = getDateStringWithFormat(NSDate(), formatString: "dd.MM.yyyy")
        self.tableData["Temp"] = "\(temp)°"
        self.tableData["Humidity"] = "\(humidity)%"
        self.tableData["Wind"] = "\(windSpeed) km/h"
    }
}

extension TableController: UITableViewDataSource, UITableViewDelegate {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.rowTitles.count ?? 0
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let myReuseIdentifier = "ReuseIdentifierPrayerTimes"
        var cell = tableView.dequeueReusableCellWithIdentifier(myReuseIdentifier) as UITableViewCell!

        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: myReuseIdentifier)
            cell.textLabel!.font = UIFont(name: cell.textLabel!.font.fontName, size: 17.0)
            cell.detailTextLabel!.font = UIFont(name: cell.detailTextLabel!.font.fontName, size: 17.0)
        }

        let rowTitle = self.rowTitles[indexPath.row]
        let rowData = self.tableData[rowTitle]

        cell!.textLabel!.text = rowTitle
        cell!.detailTextLabel!.text = rowData

        return cell
    }
}

問題是即使在主線程上調用reloadData()函數並在setWeatherData()中設置表的值后調用它,我的iPhone上的表也不會顯示數據。

如果我這樣做,表確實會顯示數據並轉到另一個選項卡並返回:

override func viewDidAppear() {
    self.ownTableView.reloadData()
}

我的問題是你是否能找到這種隨機行為的原因。 我假設在初始加載表之后reloadData()調用過快,因此被忽略。 在應用程序啟動后,如何組織API調用以及后續數據重新加載UITableView?

所以,問題是我在Xcode 7 Beta 4的Interface Builder(IB)中為我的UITableView添加了原型UITableViewCell。我不知道為什么,但是將原型UITableViewCell添加到IB中的UITableView會導致所有問題UITableViews。 在我的IB項目中的UITableViews中再次刪除原型UITableViewCells后,一切正常。 這樣我的代碼在啟動時會創建幾個UITableViewCell,並在我更改數據並在UITableView上調用reloadData()時重用它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM