2013-03-12 63 views
1

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

+0

你想只显示15条记录??? – iPatel 2013-03-12 05:45:49

+0

是的,只有前15个,然后是另外15个表滚动 – sharmaravi 2013-03-12 05:46:30

+0

而不是将其固定到15条记录,在表格中获取可见单元格并仅将数据更新到那些单元格。你可以得到这样的可见单元格信息:NSArray * visibleCells = [tableview visibleCells];请参阅此示例以进行延迟加载:http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009394-Intro-DontLinkElementID_2 – Dee 2013-03-12 05:51:58

回答

6

我想答案是:- (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... 
    } 
} 

希望这会有所帮助...! !

1

您可以使用UIScrollView这两种代表方法。由于UITableView出现在UIScrollView的层次中,因此您可以在UIScrollView的此代表方法中获得控制权。

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

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

试试以上两种方法。并选择一个适合你的。首先看看当你开始滚动UITableVIewNSLog是怎么回事。然后在你的代码里写下你的代码。

2

您必须检查UIScrollViewcontentOffset那(UITableView建立在UIScrollView上)。为了更多的细胞,当用户到达你的表视图的底部添加到您的表

- (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 
    } 
} 

:所以,这,添加到您的UITableViewDelegate类。

1

默认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

+0

你不应该只使用customcell ..你可以继续使用你的tableView。但请确保您正在重新使用不会一直创建的单元格.. – 2013-03-12 06:32:47

0

你所要做的就是懒洋洋地加载tableview数据。与当前可见的tableview单元的加载数据不同,一次不是所有的数据。

1

试试这个:

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

使用此两种方法

//for refresh 
- (void) addItemsOnTop { 

    //[self getLoginFollowingUserVideosCall]; 
    [self refreshCompleted]; 
} 

//for load more 
- (void) addItemsOnBottom { 

    //[self getLoginFollowingUserVideosCall]; 
    [self loadMoreCompleted]; 
}