0

我有一个应用程序,显示当前天气。数据通过 Alamofire 从 forecast.io 下载。结果形成一个带有预测的表格。我使用了简单的 tableviewdelegate 和 tableviewDatasource,一切正常。但现在我想学习一些反应式的,使用 rxswift 和 rxcocoa。经过一些谷歌搜索和 raywanderlich 的教程:我将代码更改为:

var week = Variable<[DailyWeather]>([])

override func viewDidLoad() {
    super.viewDidLoad()

    week.asObservable().subscribe { (e) in
        self.generateTable()
    }.addDisposableTo(disposeBag)

//        tableView.delegate = self
//        tableView.dataSource = self
        tableView.backgroundColor = UIColor(red:0.81, green:0.81, blue:0.81, alpha:1)
        refreshControl.tintColor = UIColor(red:1, green:1, blue:1, alpha:1)
        self.tableView.addSubview(self.refreshControl)
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
        updateLocation()


}

func downloadData(_ completion: @escaping DownloadComplete) {

    var weatherURL: String {
        if pre == "ru" {
            return("\(BASE_URL)\(API_KEY)/\(latitudeNew),\(longitudeNew)?units=si&lang=ru")
        } else {
            return("\(BASE_URL)\(API_KEY)/\(latitudeNew),\(longitudeNew)?units=si")
        }
    }

    print(weatherURL)

    if let url = URL(string: weatherURL) {
        let request = Alamofire.request(url)

        request.validate().responseJSON { response in
            switch response.result {
            case .success:

                self.week.value = []

                self.weekly = []
                if let data = response.result.value as! [String: AnyObject]! {

                    self.weatherDict = data["currently"] as! [String: AnyObject]!
                    self.currentDict = CurrentWeather(weatherDictionary: self.weatherDict)

                    self.dailyArray = data["daily"]?["data"] as! [[String: AnyObject]]!
                    for dailyWeather in self.dailyArray {
                        let daily = DailyWeather(dailyWeatherDict: dailyWeather)
                        self.weekly.append(daily)
                    }
                    for x in 0...7 {
                        if x == 0 {
                        } else {

                            self.week.value.append(self.weekly[x])

                        }
                    }
                    completion()
                }

            case .failure(let error):
                self.showAlert("You are offline", message: "Enable network connection and try again")
                print("Alamofire error: \(error)")
            }

        }

    }
}


func generateTable() {

    week.asObservable().bindTo(tableView.rx.items(cellIdentifier: "WeatherViewCell", cellType: WeatherViewCell.self)) { (index, weather, cell) in
        cell.configureCell(daily: weather, index: index)
        }.addDisposableTo(disposeBag)

}

但我收到这个致命错误:

fatal error: Failure converting from Optional(<UIView: 0x7facad60f620; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x60800002e520>>) to UITableViewDataSource: file /Users/ruslansabirov/Desktop/swift/Havo4/Pods/RxCocoa/RxCocoa/RxCocoa.swift, line 146
(lldb) 

请帮忙,我做错了什么?

4

2 回答 2

2

检查表视图委托和数据源方法。如果在您的代码中设置了 tableview 委托,那么您必须在使用 RXSwift 时实现 tableview 的委托方法,否则应用程序会崩溃。

于 2018-05-21T09:53:44.490 回答
0

您只需要调用一次 func generateTable() 。表格视图将自动更新。因为它是数据绑定覆盖

 func viewDidLoad() {
    super.viewDidLoad()

        self.generateTable()

        tableView.backgroundColor = UIColor(red:0.81, green:0.81, blue:0.81, alpha:1)
        refreshControl.tintColor = UIColor(red:1, green:1, blue:1, alpha:1)
        self.tableView.addSubview(self.refreshControl)
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
        updateLocation()


}
于 2017-02-13T12:14:15.513 回答