我有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)。
FUNC的tableView(的tableView:UITableView的,numberOfRowsInSection段中:int) - >詮釋{ 回報self.tableViewNews.count }這裏是numberOfRowsInSection,我已經將self.tableViewNews.addObject(newsItem)添加到數組中。此外,在行調試次數後更新爲11,所以我覺得它是確定 – nabiullinas 2015-03-03 08:03:08
@nabiullinas它還說,加/減更新,並且您已經添加了一行,在添加時使得12 :) – 2015-03-03 08:13:09
其中如果僅使用一個對於重載/插入段法/行,你不應該把它包起來,開始/結束更新 – 2015-03-03 08:17:12