0
我正在制作应用程序,它有一个tableviw,当第一次调用表显示数据完美,但当第二次时,我打开滑出菜单,然后单击按钮API再次调用,并在自我线崩溃.tableView.reloadData()。 这里是我在MainViewController应用程序获取崩溃时重新载入tabledata
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()
})
}
}
}
代码在这里是mycode的在RightViewController
@IBAction func btnmotogp(sender: AnyObject) {
slug = motogp
MainViewController().web()
self.slideMenuController()?.closeRight()
}
正好卡在这个问题上浪费太多时间在此。
这是实现代码如下
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)
}
什么是崩溃? – Paulw11
它的一个致命错误:意外地发现零,同时展开一个可选值@ Paulw11 –
你是强制unwarping的东西用'!',不使用'如果let ...',并且不应该调用不同控制器的函数,委托要求'MainViewController'来做到这一点,也许你的'tableView'没有合适的数据源,但是你仍然要更新它 – Tj3n