2015-10-18 75 views
0

我試圖用的UITableView重裝tableview中和細胞

self.tableView.reloadData() 

它可以正常工作,如果我使用數組加載靜態數據源重新載入我的表視圖。一切工作正常。 但是,當我嘗試使用我的查詢功能與解析,它加載一個新的單元格,但tableview單元格的內容不會改變。如果我重新打開應用程序,單元格會正確更新。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cellIdentifier = "EmpPostTVCellIdentifier" 
    let cell: EmpPostTVCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as? EmpPostTVCell 

    //If datasource 

    if dataSource.isEmpty{ 
     fetchDataFromParse() 
     print("no posts") 
    } 

     let itemArr:PFObject = self.dataSource[indexPath.row] 
     cell?.companyPostLabel.text = (PFUser.currentUser()?.objectForKey("companyName")!.capitalizedString)! as String 
     cell?.occupationPostLabel.text = itemArr["occupation"]!.capitalizedString as String 
     cell?.countryPostLabel.text = itemArr["country"]!.capitalizedString as String 
     let companyImage: PFFile? 
     companyImage = PFUser.currentUser()?.objectForKey("profileImageEmployer") as? PFFile 
     companyImage?.getDataInBackgroundWithBlock({ (data, error) -> Void in 
      if error == nil{ 
       cell?.companyLogoImage.image = UIImage(data: data!) 
      } 
     }) 

     let dateArr = createdByDate[indexPath.row] 
     let strDate = Settings.dateFormatter(dateArr) 

     cell?.closingDateLabel .text = strDate 



    return cell! 
} 

我使用拉使用此代碼

func refresh(sender:AnyObject) 
{ 
    dispatch_async(dispatch_get_main_queue(), {() -> Void in 
     self.fetchDataFromParse() 
     self.tableView.reloadData() 
     self.refreshControl?.endRefreshing() 
    }) 

} 

帶或不帶dispatch_asynch功能的結果刷新我的tableviews內容保持不變。它只是添加新的tableviewcell,但其中的內容不會改變。任何想法傢伙?

編輯1:

func fetchDataFromParse() { 
    //  MARK: - JOB POST QUERY 
    if PFUser.currentUser()?.objectId == nil{ 
     PFUser.currentUser()?.saveInBackgroundWithBlock({ (success, error) -> Void in 
      let query = PFQuery(className: "JobPost") 
      //creating a pointer 
      let userPointer = PFUser.objectWithoutDataWithObjectId(PFUser.currentUser()?.objectId) 

      query.whereKey("postedBy", equalTo: userPointer) 

      query.orderByDescending("createdAt") 
      let objects = query.findObjects() 
      for object in (objects as? [PFObject])!{ 
       //print(object.objectId) 
       self.dataSource.append(object) 
       self.createdByDate.append((object.objectForKey("closingDate") as? NSDate)!) 
       print(self.dataSource) 
       print(self.createdByDate) 
      } 
     }) 
    } else { 
     let query = PFQuery(className: "JobPost") 
     //creating a pointer 
     let userPointer = PFUser.objectWithoutDataWithObjectId(PFUser.currentUser()?.objectId) 

     query.whereKey("postedBy", equalTo: userPointer) 

     query.orderByDescending("createdAt") 
     let objects = query.findObjects() 
     for object in (objects as? [PFObject])!{ 
      //print(object.objectId) 
      self.dataSource.append(object) 
      self.createdByDate.append((object.objectForKey("closingDate") as? NSDate)!) 
      print(self.dataSource) 
      print(self.createdByDate) 

     } 
    }//end of PFUser objectID == nil else clause 
} 

回答

1

讓我們來看看fetchDataFromParse()函數的內容,我想你會填充self.dataSource陣列

+0

更新了我的問題。 – suisied

+0

試試這個而不是tableview.reloadData() - self.tableView.reloadSections(NSIndexSet(index:0),withRowAnimation:UITableViewRowAnimation.None) – olucurious

+0

你應該嘗試把self.tableView.reloadData()和 self.refreshControl ?.在附加到數據源數組的for循環之後的fetchDataFromParse()函數中的endRefreshing(),這樣只會在數據被追加後重新加載。 – olucurious

0

嘗試調用self.tableview.reloadData()當fetchDataFromParse()完成時。

+0

這就是上面的代碼。 – suisied

0

檢查你的數據源陣列是在你的fetchDataFromParse方法

+0

我想不是這樣,因爲當我重新打開應用程序,tableviewcells更新和罰款。 – suisied

0

PFUser.currentUser()的端部空?saveInBackgroundWithBlock是asynchronus方法。所以你的tableView單元沒有數據。

+0

任何樣本解決方案都會執行:/ – suisied

+0

,只有當用戶爲零時纔會運行此行,以避免錯誤「未保存的對象」。你應該停止指出,而不支持你的答案。 – suisied