1

我有 2000 条记录,我只需要显示 15 条记录UITableView,当用户滚动UITableView时,更多的 15 条记录应该加载到表中。我该怎么做?

4

6 回答 6

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

希望对您有所帮助...!!!

于 2013-03-12T07:00:40.710 回答
2

你必须检查UIScrollView contentOffset那个(UITableView建立在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   
    }
}

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

于 2013-03-12T05:51:36.987 回答
1

您可以使用两种委托方法UIScrollView。正如UITableView在层次结构中一样,UIScrollView您将在这个委托方法中获得控制权UIScrollView

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

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

试试以上两种方法。并选择适合您的那一款。首先看看NSLog当你开始滚动你的UITableVIew. 然后在其中编写您的代码。

于 2013-03-12T05:50:28.940 回答
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

于 2013-03-12T06:03:48.620 回答
1

试试这个:

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

使用这两种方法

//for refresh
- (void) addItemsOnTop {

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

//for load more
- (void) addItemsOnBottom  {

     //[self getLoginFollowingUserVideosCall];
     [self loadMoreCompleted];
}
于 2013-03-12T06:10:08.260 回答
0

您要做的是懒惰地加载 tableview 数据。就像加载当前可见的表格视图单元格的数据,而不是一次所有数据。

于 2013-03-12T06:03:57.533 回答