简体   繁体   English

重新加载表数据时应用程序崩溃

[英]application getting crash when reload tabledata

i am making app which have a tableviw and when first time api called table showing data perfectly but when secondtime i open slide out menu and click on button api is calling again and its getting crash on line self.tableView.reloadData(). 我正在制作一个具有tableviw的应用程序,当第一次使用api调用table可以完美显示数据,但是当我第二次打开幻灯片菜单并单击按钮api再次调用时,其self.tableView.reloadData()行崩溃了。 here is my code in MainViewController 这是我在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()
            })


        }
    }
}

here is mycode in RightViewController 这是RightViewController中的mycode

@IBAction func btnmotogp(sender: AnyObject) {
    slug = motogp
   MainViewController().web()

    self.slideMenuController()?.closeRight()


}

just stuck in this problem and wasted too much time on this. 只是停留在这个问题上,浪费了太多时间。

this is the code for tableview 这是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)
}

It is a bit hard to find out what is where and how exactly you uses it but my guess is that when you call MainViewController().web() the MainViewController's view is not in the views hierarchy so tableView is neither. 很难确定您在哪里使用它以及如何使用它,但是我的猜测是,当您调用MainViewController().web() ,MainViewController的视图不在视图层次结构中,因此tableView都不在视图层次结构中。 This provides us to the case when you are calling table to reload but it is not instantiated so it crashes. 这为我们提供了一种情况,当您调用表进行重新加载但未实例化因此崩溃时。 Make sure that view controller is added correctly. 确保正确添加了视图控制器。

It would be also nice if you could care about possible retain cycles that blocks can create :) 如果您可以关心块可以创建的可能的保留周期,那也很好:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM