2013-03-12 47 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]; 
}