在Uitableview上滚动数据

我有2000条logging,我只能在UITableView显示15条logging,当用户滚动UITableView ,更多15条logging被载入表中。 我应该怎么做?

我认为答案是: - (void)scrollViewDidScroll:(UIScrollView *)scrollView

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint offset = scrollView.contentOffset; CGRect bounds = scrollView.bounds; CGSize size = scrollView.contentSize; UIEdgeInsets inset = scrollView.contentInset; float y = offset.y + bounds.size.height - inset.bottom; float h = size.height; float reload_distance = 15; if(y > h + reload_distance) { //Call the Method to load More Data... } } 

希望它会有帮助…!

你必须检查UIScrollView contentOffsetUITableViewbuild立在UIScrollView )。 所以,把这个添加到你的UITableViewDelegate类中:

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // when reaching bottom, load more float offs = (scrollView.contentOffset.y+scrollView.bounds.size.height); float val = (scrollView.contentSize.height); if (offs==val) { //add more data on your data array } } 

为了添加更多的细胞到你的表,当用户达到你的表视图的底部。

你可以使用UIScrollView两个委托方法。 当UITableView进入UIScrollView层次时,你将获得UIScrollView这个委托方法的控制权。

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { NSLog(@"TableView scrolling"); } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { NSLog(@"TableView Started scrolling"); } 

尝试以上两种方法。 并select一个适合你的。 首先看看当你开始滚动你的UITableVIewNSLog是怎么来的。 然后把你的代码写在里面。

默认情况下numberOfItemsToDisplay = 15;

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (numberOfItemsToDisplay >= [self.yourArray count]) { numberOfItemsToDisplay = [self.yourArray count]; return 1; } return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { return numberOfItemsToDisplay; } else { return 1; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { static NSString *CellIdentifier = @"YourCustomCell"; YourCustomCell *objYourCustomCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (objYourCustomCell == nil) { NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"YourCustomCell" owner:self options:nil]; for(id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[YourCustomCell class]]) { objYourCustomCell = (AlertsCustomCell *) currentObject; break; } } } objYourCustomCell.lbl.text = [[self.yourArray objectAtIndex:indexPath.row]valueForKey:@"vName"]; return objYourCustomCell; } else { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; [cell.contentView addSubview:[self loadMoreViewForTable:self.view]]; } return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tblAlertsList deselectRowAtIndexPath:indexPath animated:YES]; if (indexPath.section == 1) { numberOfItemsToDisplay = [self loadMore:numberOfItemsToDisplay arrayTemp:self.yourArray tblView:self.tblAlertsList]; [self.tblAlertsList endUpdates]; }else { // action if it is not LoadMore } } + (NSInteger)loadMore : (NSInteger)numberOfItemsToDisplay arrayTemp:(NSMutableArray*)aryItems tblView:(UITableView*)tblList { int count =0; NSUInteger i, totalNumberOfItems = [aryItems count]; NSUInteger newNumberOfItemsToDisplay = MIN(totalNumberOfItems, numberOfItemsToDisplay + kNumberOfItemsToAdd); NSMutableArray *indexPaths = [[NSMutableArray alloc] init]; for (i=numberOfItemsToDisplay; i<newNumberOfItemsToDisplay; i++) { count++; [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; } numberOfItemsToDisplay = newNumberOfItemsToDisplay; [tblList beginUpdates]; [tblList insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft]; if (numberOfItemsToDisplay == totalNumberOfItems) { [tblList deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationLeft]; } NSIndexPath *scrollPointIndexPath; if (newNumberOfItemsToDisplay < totalNumberOfItems) { scrollPointIndexPath = [NSIndexPath indexPathForRow:numberOfItemsToDisplay-kNumberOfItemsToAdd inSection:0]; } else { scrollPointIndexPath = [NSIndexPath indexPathForRow:i-count inSection:0]; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100000000), dispatch_get_main_queue(), ^(void){ [tblList scrollToRowAtIndexPath:scrollPointIndexPath atScrollPosition:UITableViewScrollPositionNone animated:YES]; }); return numberOfItemsToDisplay; } 

我必须采取自定义单元格使用此方法? 我只采取了UItableView

尝试这个:

http://www.cocoacontrols.com/controls/stableviewcontroller

使用这两种方法

 //for refresh - (void) addItemsOnTop { //[self getLoginFollowingUserVideosCall]; [self refreshCompleted]; } //for load more - (void) addItemsOnBottom { //[self getLoginFollowingUserVideosCall]; [self loadMoreCompleted]; } 

你必须做的是懒洋洋地加载tableview数据。 就像当前可见的tableview单元的加载数据一样,而不是一次的所有数据。