【发布时间】:2016-03-03 10:51:28
【问题描述】:
我正在制作具有 tableviw 的应用程序,当第一次 api 调用 table 时完美显示数据但是当我第二次打开滑出菜单并单击按钮时,api 再次调用并且它在 self.tableView.reloadData() 线上崩溃. 这是我在 MainViewController 中的代码
func web()
{
let url = "http://\(urlString)\(slug)"
print(url)
print(slug)
request(.GET, url, parameters: nil, encoding: .JSON).responseJSON { (response:Response<AnyObject, NSError>) -> Void in
if (response.result.value != nil)
{
print(response.result.value)
self.arraypost.removeAllObjects()
print(self.arraypost)
self.arraypost = (response.result.value)?.valueForKey("posts") as! NSMutableArray
print(self.arraypost)
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.tableView.reloadData()
})
}
}
}
这是我在 RightViewController 中的代码
@IBAction func btnmotogp(sender: AnyObject) {
slug = motogp
MainViewController().web()
self.slideMenuController()?.closeRight()
}
卡在这个问题上,浪费了太多时间。
这是tableview的代码
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arraypost.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : mainviewcell = tableView.dequeueReusableCellWithIdentifier("mainviewcell") as! mainviewcell
let dic : NSDictionary = arraypost.objectAtIndex(indexPath.row) as! NSDictionary
cell.lbldate.text = dic["date"] as? String
cell.lblsummary.text = dic["excerpt"] as? String
cell.lbltitle.text = dic["title"] as? String
let myarray : NSMutableArray = (dic["attachments"] as? NSMutableArray)!
print(myarray)
let dic1 : NSDictionary = myarray.objectAtIndex(0) as! NSDictionary
print(dic1)
var newimage : String = ""
newimage = dic1["url"] as! String
print(newimage)
if (newimage.characters.count != 0)
{
ImageLoader.sharedLoader.imageForUrl(newimage) { (images, url) -> () in
if (images != nil)
{
cell.image1.image = images!
}
}
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let storyboard = UIStoryboard(name: "SubContentsViewController", bundle: nil)
let subContentsVC = storyboard.instantiateViewControllerWithIdentifier("SubContentsViewController") as! SubContentsViewController
self.navigationController?.pushViewController(subContentsVC, animated: true)
}
【问题讨论】:
-
这是一个致命错误:在展开可选值时意外发现 nil@Paulw11
-
您正在使用
!强制取消扭曲,不要,使用if let...,并且不应该从不同的控制器调用函数,请委托MainViewController执行此操作,也许是您的@987654327 @ 还没有正确的数据源,但你还是调用了更新它
标签: ios swift uitableview tableview ios9