2015-03-03 90 views
0

我有tableView,從10個項目的url api提供數據。當它向下滾動時,我嘗試獲取下10個項目並將它們插入到行中。 所以在viewDidLoad中,我得到插入行到UItableView錯誤

Alamofire.request(.GET, urlQuery, parameters: ["universityId": UBUniversitySettings.getUniversityId(), "page" : currentPage, "pageSize" : 10]).responseJSON{ (request, response, JSON, error) in 
      println(JSON) 
      var newsResponse = JSON! as NSArray 
      for newsItemResponse in newsResponse{ 
       var newsItem:NewsModel = NewsModel() 
       newsItem.newsImage = newsItemResponse.valueForKey("previewPic") as? String 
       newsItem.newsTitle = newsItemResponse.valueForKey("title") as? String 
       newsItem.newsDescription = newsItemResponse["content"] as? String 
       newsItem.newsId = newsItemResponse["id"] as? String 
       self.tableViewNews.addObject(newsItem) 

       // self.newsTableView.reloadData() 
       self.newsTableView.reloadSections(NSIndexSet.init(index: 0), withRowAnimation:UITableViewRowAnimation.Bottom) 


      } 
      self.currentPage = self.currentPage + 1 

     } 

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    if(indexPath.row == self.currentPage * 10 - 1){ 
     self.getNextNews() 
    } 
} 

我得到下一個項目

func getNextNews(){ 
    Alamofire.request(.GET, urlQuery, parameters: ["universityId": UBUniversitySettings.getUniversityId(), "page" : currentPage, "pageSize" : 10]).responseJSON{ (request, response, JSON, error) in 
     println(JSON) 
     var newsResponse = JSON! as NSArray 
     if(newsResponse.count > 0){ 
      var newsIndex:NSInteger = 0 
      var paths:NSMutableArray = NSMutableArray() 
      for newsItemResponse in newsResponse{ 

       var newsItem:NewsModel = NewsModel() 
       newsItem.newsImage = newsItemResponse.valueForKey("previewPic") as? String 
       newsItem.newsTitle = newsItemResponse.valueForKey("title") as? String 
       newsItem.newsDescription = newsItemResponse["content"] as? String 
       newsItem.newsId = newsItemResponse["id"] as? String 
       self.tableViewNews.addObject(newsItem) 

       // self.newsTableView.reloadData() 
       self.newsTableView.reloadSections(NSIndexSet.init(index: 0), withRowAnimation:UITableViewRowAnimation.Bottom) 
       var indexPathVal = self.currentPage * 10 + newsIndex 

       var indexPath = NSIndexPath(forRow: indexPathVal as Int, inSection: 0) 
       paths.addObject(indexPath) 
       newsIndex = newsIndex + 1 

      } 
      self.newsTableView.reloadData() 
      self.newsTableView.beginUpdates() 

      self.newsTableView.insertRowsAtIndexPaths(paths, withRowAnimation: UITableViewRowAnimation.Bottom) 
      self.currentPage = self.currentPage + 1 
      self.newsTableView.endUpdates() 



     } 

    } 
} 

But my app crashed and give me a message 

無效更新:在0節中包含的行數行數無效更新之後的現有部分(11)必須等於更新前(11)前部分中包含的行數,加上或減去從該部分插入或刪除的行數(插入1個,刪除0個),加上或減去移入或移出該部分的行數(移入0,移出0)。

回答

0

之後調用self.newsTableView.reloadData() tableView已經包含新數據。當您嘗試更新: self.newsTableView.beginUpdates() //代碼 self.newsTableView.endUpdates() 您的數組未更改。在數組更新時,您不應該調用reloadData或reloadSections(如果需要,可以在函數結束時使用插入動畫)。或者你應該使用reloadData或reloadSections而不用開始/結束更新。

你的方法應該是:

func getNextNews(){ 
    Alamofire.request(.GET, urlQuery, parameters: ["universityId": UBUniversitySettings.getUniversityId(), "page" : currentPage, "pageSize" : 10]).responseJSON{ (request, response, JSON, error) in 
     println(JSON) 
     var newsResponse = JSON! as NSArray 
     if(newsResponse.count > 0){ 
      //code 
      self.tableViewNews.addObject(newsItem) 

      //self.newsTableView.reloadSections(NSIndexSet.init(index: 0), withRowAnimation:UITableViewRowAnimation.Bottom) 

     } 
     //self.newsTableView.reloadData() 
     self.newsTableView.beginUpdates() 

     self.newsTableView.insertRowsAtIndexPaths(paths, withRowAnimation: UITableViewRowAnimation.Bottom) 
     self.currentPage = self.currentPage + 1 
     self.newsTableView.endUpdates() 
     } 
    } 
} 

或者:

 func getNextNews(){ 
    Alamofire.request(.GET, urlQuery, parameters: ["universityId": UBUniversitySettings.getUniversityId(), "page" : currentPage, "pageSize" : 10]).responseJSON{ (request, response, JSON, error) in 
     println(JSON) 
     var newsResponse = JSON! as NSArray 
     if(newsResponse.count > 0){ 
      //code 
      self.tableViewNews.addObject(newsItem) 

      self.newsTableView.reloadSections(NSIndexSet.init(index: 0), withRowAnimation:UITableViewRowAnimation.Bottom) 

     } 
     self.newsTableView.reloadData() 
     //self.newsTableView.beginUpdates() 

     //self.newsTableView.insertRowsAtIndexPaths(paths, withRowAnimation: UITableViewRowAnimation.Bottom) 
     self.currentPage = self.currentPage + 1 
     //self.newsTableView.endUpdates() 
     } 
    } 
} 
0

查看您的代碼。您是否在細胞切片中返回正確數量的細胞?

此外,包裝你插入代碼

[tableView beginUpdate]; 
// Do insertion here. 
    [tableview endUpdate]; 
    [tableView reloadData]; 

而在你的代碼中,行

self.newsTableView.reloadSections(NSIndexSet.init(index: 0), withRowAnimation:UITableViewRowAnimation.Bottom) 

不裹endUpdate /的BeginUpdate

而在你的最後一個函數,你reloadData也不在包裝中。

+0

FUNC的tableView(的tableView:UITableView的,numberOfRowsInSection段中:int) - >詮釋{ 回報self.tableViewNews.count }這裏是numberOfRowsInSection,我已經將self.tableViewNews.addObject(newsItem)添加到數組中。此外,在行調試次數後更新爲11,所以我覺得它是確定 – nabiullinas 2015-03-03 08:03:08

+0

@nabiullinas它還說,加/減更新,並且您已經添加了一行,在添加時使得12 :) – 2015-03-03 08:13:09

+0

其中如果僅使用一個對於重載/插入段法/行,你不應該把它包起來,開始/結束更新 – 2015-03-03 08:17:12