在Uitableview滚动上加载数据

时间:2013-03-12 05:43:15

标签: iphone ios objective-c uitableview

我有2000条记录,我必须在UITableView中只显示15条记录,当用户滚动UITableView时,需要在表格中加载更多15条记录。我该怎么办?

6 个答案:

答案 0 :(得分:5)

我认为答案是:- (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 :(得分:2)

您必须检查UIScrollView contentOffsetUITableView建立在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   
    }
}

当用户到达表格视图的底部时,为了向表格中添加更多单元格。

答案 2 :(得分:1)

您可以使用UIScrollView的两种委托方法。当UITableView出现在UIScrollView的层次结构中时,您将获得UIScrollView的委托方法的控制权。

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

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

尝试以上两种方法。并选择适合您的。首先看看当您开始滚动NSLogUITableVIew的来历。然后在其中编写代码。

答案 3 :(得分: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

答案 4 :(得分:1)

试试这个:

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

使用这两种方法

//for refresh
- (void) addItemsOnTop {

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

//for load more
- (void) addItemsOnBottom  {

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

答案 5 :(得分:0)

您需要做的是懒洋洋地加载tableview数据。类似于当前可见的tableview单元的加载数据&amp;并非一次全部数据。