首先执行代码!迅速

时间:2016-10-14 22:11:39

标签: ios swift uitableview swift3

伙计我从Foursquare APi获取数据,这里是我的代码。

但是我在cellForRowAtIndexPath上得到一个nil错误,因为venueItems是nil

    proc->tf->esp = proc->tf->esp + 24;
    *((uint*)(proc->tf->esp - 16)) = proc->tf->esp;
    *((uint*)(proc->tf->esp - 12)) = proc->tf->esp;
    *((uint*)(proc->tf->esp - 8)) = proc->tf->esp;
    proc->tf->eip = *((uint*)(proc->tf->esp - 4));

我个人编程很新我认为venueItems是nil,因为class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // Table View self.tableView = UITableView() // Location Manager Stuff self.locationManager = CLLocationManager() self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters self.locationManager.delegate = self let status = CLLocationManager.authorizationStatus() if status == .notDetermined { self.locationManager.requestWhenInUseAuthorization() } else if status == CLAuthorizationStatus.authorizedWhenInUse || status == CLAuthorizationStatus.authorizedAlways { self.locationManager.startUpdatingLocation() } else { showNoPermissionsAlert() } exploreVenues() } // Func's func exploreVenues() { guard let location = self.locationManager.location else { return } var parameters = [Parameter.query: "Pubs"] parameters += location.parameters() let task = self.session.venues.explore(parameters) { (result) -> Void in if self.venueItems != nil { return } if !Thread.isMainThread { fatalError("!!!") } if let response = result.response { if let groups = response["groups"] as? [[String: AnyObject]] { var venues = [[String: AnyObject]]() for group in groups { if let items = group["items"] as? [[String: AnyObject]] { venues += items } } self.venueItems = venues } self.tableView.reloadData() } else if let error = result.error, !result.isCancelled() { self.showErrorAlert(error) } } task.start() } // Table View Data source func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if let venueItems = self.venueItems { return venueItems.count } return 10 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! VenueTableViewCell // This is where the error occurs let item = self.venueItems![(indexPath as NSIndexPath).row] as JSONParameters! self.configureCellWithItem(cell, item: item!) return cell } func configureCellWithItem(_ cell: VenueTableViewCell, item: JSONParameters) { if let venueInfo = item["venue"] as? JSONParameters { cell.nameLabel.text = venueInfo["name"] as? String } } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { let cell = cell as! VenueTableViewCell let tips = self.venueItems![(indexPath as NSIndexPath).row]["tips"] as? [JSONParameters] guard let tip = tips?.first, let user = tip["user"] as? JSONParameters, let photo = user["photo"] as? JSONParameters else { return } let URL = photoURLFromJSONObject(photo) if let imageData = session.cachedImageDataForURL(URL) { cell.venueImageView.image = UIImage(data: imageData) } else { cell.venueImageView.image = nil session.downloadImageAtURL(URL) { (imageData, error) -> Void in let cell = tableView.cellForRow(at: indexPath) as? VenueTableViewCell if let cell = cell, let imageData = imageData { let image = UIImage(data: imageData) cell.venueImageView.image = image } } } } } 首先被执行。如果这是错误,我该如何修复它,以便在我的venueItems有一个值或任何其他更有效的方法之后运行cellForRowAtIndexpath中的代码?

1 个答案:

答案 0 :(得分:4)

numberOfRowsInSection为零时,10会返回self.venueItemsself.venueItems似乎是零,直到您的网络请求完成,因此表视图已被告知它有10行显示要求每行的单元格。然后,您尝试强制解包可选属性(self.venueItems!)并崩溃。

看起来您的self.venueItems是可选的,有充分理由,请勿使用强制解包(!)丢弃该信息。当此属性为nil时,您可以返回0行,也可以将其初始化为非可选的空数组,然后您可以随时询问其count

一般来说,这类问题你不想专注于阻止cellForRowAtIndexPath被调用,而是计划在任何时候调用它并返回一个合理的结果(比如报告该表有当你的后台任务还没有完成时,这就是0行。

相关问题