2015-11-12 58 views
1

我正在研究一個應用程序,該應用程序從數據庫中檢索用戶的朋友,然後在表格視圖中輸出。運行良好的代碼在viewDidLoad()中使用時崩潰應用程序

我已成功將表視圖編碼爲讀取我插入到NSMutableArray()中的索引的位置。我的想法是使用NSURL通過使用PHP的URL變量向MySQL數據庫發送查詢。我使用NSURL多次與數據庫進行交互,但是當我在viewDidLoad()函數中使用它來立即在應用程序加載時加載好友時,它崩潰但不返回錯誤。

代碼:

class viewFriendsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet weak var tableView: UITableView! 

    var textArray: NSMutableArray = NSMutableArray() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let myUrl = NSURL(string: "http://www.casacorazon.org/ios.html") 
     let request = NSMutableURLRequest(URL: myUrl!) 
     request.HTTPMethod = "POST" 
     let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { 
      data, response, error in 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) { 
       let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) 
       if error != nil { 
        print("Error: \(error)") 
       } 
       dispatch_async(dispatch_get_main_queue()) { 
        print(responseString) 
       } 
      } 
     } 
     task.resume() 
     //get username from NSUserDefaults 
      //if username inavailable, insert error report into first row 
     //use PHP script to get friends from user's database 
     //split return string by '9245203598' into array 
     //load split array into NSMutableArray via foreach loop 
     //let username = NSUserDefaults.standardUserDefaults().stringForKey("username")*/ 
     self.textArray.addObject("First Index") 
     self.textArray.addObject("Second Index") 
     self.textArray.addObject("Third Index") 
     self.tableView.rowHeight = UITableViewAutomaticDimension 
     self.tableView.estimatedRowHeight = 44.0 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.textArray.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell 
     cell.textLabel?.text = self.textArray.objectAtIndex(indexPath.row) as? String 
     return cell 
    } 

    func sendAlert(subject: String, message: String) { 
     let alertController = UIAlertController(title: subject, message: 
      message, preferredStyle: UIAlertControllerStyle.Alert) 
     alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil)) 

     self.presentViewController(alertController, animated: true, completion: nil) 
    } 
} 
+0

你有沒有嘗試將代碼移到viewWillAppear? – RichAppz

+0

你應該使用一些錯誤處理。 – lukaivicev

+0

你調試了嗎?設置斷點並查看哪些值爲零。 – lukaivicev

回答

2

,因爲你的應用程序完全excuting在UI代碼後臺完成其任務之前。您必須確保後臺任務已完成,然後在用戶界面中繼續。

注意:NSUrlsesstion在後臺運行,您不必在其中導入dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0)){}。

+1

我猜猜同樣的事情。使用錯誤處理@Nickpitoniak。 – lukaivicev

相關問題