- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
伙计们,我正在从 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/
几个月前,我做了一个功能,我的应用程序正在等待用户文档并做出相应的响应。直到我对项目进行优化并将其更新到最新版本之前,它一直是一种魅力。 如果存在用户文档,则流将产生该文档并关闭该流。 如果云Fire
Stack Overflow 有几个 examples其中函数首先获得可升级锁,然后通过升级获得独占访问。我的理解是,如果不小心使用,这可能会导致死锁,因为两个线程可能都获得了可升级/共享锁,然后都尝
这个问题在这里已经有了答案: MVC 4 Code First ForeignKeyAttribute on property ... on type ... is not valid (1 个回答
以下是部分代码。我需要在 finally 子句中关闭资源。我需要先调用 closeEntry() 还是 close()?我收到一些错误消息。 Error closing the zipoutjava.
我想使用 RxJS-DOM 观察 mousewheel 事件,这样当第一个事件触发时,我转发它然后删除所有值,直到后续值之间的延迟超过先前指定的持续时间。 我想象的运算符可能看起来像: Rx.DOM.
版本似乎与安装的不同。 我在 npm install 上收到警告 我将二进制文件安装到我的家庭/开发目录中,但它不适用于 sudo。所以我安装了apt。 (注意:我并没有真正安装,我提取并将路径放在/
我正在尝试展示 GAN 网络在某些指定时期的结果。打印当前结果的功能以前与 TF 一起使用。我需要换成 pytorch。 def show_result(G_net, z_, num_epoch, s
我是一名优秀的程序员,十分优秀!