gpt4 book ai didi

ios - 先执行代码! swift

转载 作者:行者123 更新时间:2023-11-28 10:17:05 25 4
gpt4 key购买 nike

伙计们,我正在从 Foursquare APi 获取数据,下面是我的代码。

但我在 cellForRowAtIndexPath 处收到一个 nil 错误,表示 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 是 nil,因为 cellForRowAtIndexPath 首先被执行。如果这是错误,我该如何修复它,以便 cellForRowAtIndexpath 中的代码在我的 venueItems 具有值之后运行......或任何其他更有效的方式?

最佳答案

self.venueItems 为 nil 时,您的 numberOfRowsInSection 返回 10。在您的网络请求完成之前,self.venueItems 似乎为 nil,因此表格 View 被告知它有 10 行要显示,要求每行一个单元格。然后,您尝试强制展开可选属性 (self.venueItems!) 并崩溃。

看起来您的 self.venueItems 是可选的,这是有充分理由的,不要用强制展开 (!) 丢弃该信息。您可以在该属性为 nil 时返回 0 行,或者将其初始化为一个非可选的空数组,然后您可以随时请求它的 count

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

关于ios - 先执行代码! swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40052707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com