UITableView分页 – 底部刷新以加载Swift中的新数据

我试图实现从我的后端分页加载数据。 我已经看到了这一点,但它加载所有的数据,所有的时间。 这是正确的方式还是我做错了什么?

提前致谢。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell print(indexPath.row) if (indexPath.row + 1 < self.TotalRowsWithPages) { cell.textLabel?.text = "\(self.myarray!.[indexPath.row].body)" } else { cell.textLabel?.text = "Loading more data..."; // User has scrolled to the bottom of the list of available data so simulate loading some more if we aren't already if (!self.isLoading) { self.isLoading = true; self.TotalRowsWithPages = self.TotalRowsWithPages + self.PageSize self.getmoredata() } } return cell } 

不,你不能用这种方法去,因为cellForRowAtIndexPath被调用了很多次,同时也需要很多时间来检查你的条件!

在这里,我已经find了一个更好的selectUITableView分页。

 func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { //Bottom Refresh if scrollView == tableView{ if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height) { if !isNewDataLoading{ if helperInstance.isConnectedToNetwork(){ isNewDataLoading = true getNewData() } } } } } 

isNewDataLoadingBool来检查UITableView是否正在加载新的数据!

希望这可以帮助!

你应该在tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)实现更多的加载。 当最后一个单元格加载显示时,它意味着用户滚动到底部,所以这是您需要加载更多的数据的时间。

也许它看起来像这样:

 func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { if !(indexPath.row + 1 < self.TotalRowsWithPages) { self.isLoading = true; self.getmoredata() } } 

尝试检查更多的数据不在cellForRowAtIndexPath但在UIScrollViewDelegate – [DataModel sharedMyLibrary]是我的数据源类加载video数据使用RESTful API与分页,它的fetchWithCompletion方法从asynchronous服务器获取数据,hasMore方法说,服务器有更多的数据JSON包含下一个链接)LibraryTableViewController – 是UITableViewController的子类,hasMore – 是故事板中包含一个button的表视图底部的视图,所以用户有两个选项:向下滚动或按下button。 同样,如果这个视图是可见的,则表明服务器上有更多的数据。 _canFetch防止来自服务器的嵌套加载。

 @interface LibraryTableViewController () <UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIView *hasMore; @end @implementation LibraryTableViewController { __block volatile uint8_t _canFetch; } @synthesize hasMore = _hasMore; - (void)viewDidLoad { _canFetch = 0x80; [super viewDidLoad]; [self fetchVideos:NO]; } - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { CGPoint offset = [scrollView contentOffset]; if ([[DataModel sharedMyLibrary] hasMore]) { if (((velocity.y > 0.0) && (offset.y > (*targetContentOffset).y)) || ((velocity.x > 0.0) && (offset.x > (*targetContentOffset).x))) { [self fetchVideos:NO]; } } else { [_hasMore setHidden:YES]; } } - (IBAction)moreVideos:(UIButton *)sender { [self fetchVideos:NO]; } - (IBAction)doRefresh:(UIRefreshControl *)sender { [sender endRefreshing]; [[DataModel sharedMyLibrary] clear]; [self fetchVideos:YES]; } - (void)fetchVideos:(BOOL)reload { if (OSAtomicTestAndClear(0, &(_canFetch))) { __weak typeof(self) weakSelf = self; [[DataModel sharedMyLibrary] fetchWithCompletion:^(NSArray *indexPathes) { dispatch_async(dispatch_get_main_queue(), ^{ __strong typeof(self) strongSelf = weakSelf; if (indexPathes != nil) { if (reload) { [[strongSelf tableView] reloadData]; } else { [[strongSelf tableView] beginUpdates]; [[strongSelf tableView] insertRowsAtIndexPaths:indexPathes withRowAnimation:UITableViewRowAnimationAutomatic]; [[strongSelf tableView] endUpdates]; } } else { [[strongSelf tableView] reloadData]; } [strongSelf->_hasMore setHidden:![[DataModel sharedMyLibrary] hasMore]]; strongSelf->_canFetch = 0x80; }); }]; } }