簡體   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