繁体   English   中英

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

[英]application getting crash when reload tabledata

我正在制作一个具有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中的mycode

@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)
}

很难确定您在哪里使用它以及如何使用它,但是我的猜测是,当您调用MainViewController().web() ,MainViewController的视图不在视图层次结构中,因此tableView都不在视图层次结构中。 这为我们提供了一种情况,当您调用表进行重新加载但未实例化因此崩溃时。 确保正确添加了视图控制器。

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

暂无
暂无

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

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