【问题标题】:application getting crash when reload tabledata重新加载表数据时应用程序崩溃
【发布时间】: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


【解决方案1】:

很难找出你在哪里以及如何使用它,但我猜当你调用MainViewController().web() 时,MainViewController 的视图不在视图层次结构中,所以 tableView 也不在。这为我们提供了当您调用 table 以重新加载但它没有实例化因此它崩溃的情况。确保正确添加视图控制器。

如果你能关心块可以创建的可能的保留周期,那也很好:)

【讨论】:

    猜你喜欢
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多