2015-10-16 51 views
0

由於某種原因數組沒有被放入表視圖任何原因爲什麼? CellWithIdentifier已正確實例化,故事板中的單元格具有相同的標識符。還確認該陣列已填充。我也已經連接故事板上的數據源和委託。爲什麼tableview不顯示數組?爲什麼數組不在tableview中?

let TextQuery = PFQuery(className: "submit") 
    TextQuery.findObjectsInBackgroundWithBlock({ 
     (objects: [AnyObject]?, error: NSError?) -> Void in 

     var object = objects as! [PFObject] 

     for i in 0...object.count-1{ 
      self.arrayOftexts.append(object[i].valueForKey("text") as! String) 
      print(self.arrayOftexts) 
     } 

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = myTableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 
    cell.textLabel?.text = arrayOftexts[indexPath.row] 
    print("added to tableview") 
    return cell 
} 
+0

你根本調用'tableview.reloadData()'嗎? (應該是你的for循環之後大概) – Fonix

+0

你輸入了下面的方法嗎? func numberOfSectionsInTableView(tableView:UITableView) - > Int { return 1 } – Diego

回答

0

你忘記添加self.tableView.reloadData, - >這是刷新數據

for i in 0...object.count-1{ 
     self.arrayOftexts.append(object[i].valueForKey("text") as! String) 
     print(self.arrayOftexts) 
    } 
// add the following line 
dispatch_async(dispatch_get_main_queue(), {() -> Void in 
self.tableView.reloadData() 
}) 

選擇2

for i in 0...object.count-1{ 
     self.arrayOftexts.append(object[i].valueForKey("text") as! String) 
     print(self.arrayOftexts) 
    } 


// add the following line 

if self.arrayOftexts>0 
{ 
dispatch_async(dispatch_get_main_queue(), {() -> Void in 
self.tableView.reloadData() 
}) 
} 
else 
{ 
// set to tableview as hidden 
} 

override func numberOfSectionsInTableView(tableView: UITableView!) -> Int { 

    return 1 
} 
0

因爲每個人都有說,你很可能缺少tableView.reloadData()

我最常做的是這樣的:

var arrayOftexts : [AnyObject]? { 
    didSet { 
     tableView.reloadData() 
    } 
} 

這將讓你重新加載表時數組被更新。