UItableView在ios中滚动加载数据

时间:2015-12-28 11:03:29

标签: ios objective-c uitableview

嗨,在我的数组中有大量数据,我必须在UITableView中显示它。但这里的条件是我最初只需显示5条记录,然后一旦用户向下滚动我必须加载更多记录。我尝试搜索它但没有得到任何有用的答案请帮助我,我同意我们有使用((void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath )

另一种方式

我们可以调整tableView:numberOfRowsInSection:方法的返回值,每次我们要插入十行时,你可以加5来返回值。

但我不明白该怎么做才能帮助我

我的代码:

   #import "TableViewController.h"

@interface TableViewController ()
{
    UITableView * tableView;
    NSUInteger reloads_;
    NSMutableArray * MainArray;
    int  scrollValue;
    long dataLimit;
    NSArray * yourDataSource;
}

@end

@implementation TableViewController
- (void)viewDidLoad
{
    [super viewDidLoad];

    MainArray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil];

    tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tableView];
}

#pragma mark - UITableViewDataSource
// number of section(s), now I assume there is only 1 section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
    return 1;
}

// number of row in the section, I assume there is only 1 row
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{

    return yourDataSource.count;
}

// the cell will be returned to the tableView
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"newFriendCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [yourDataSource objectAtIndex:indexPath.row];

    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    [self estimatedTotalData];
}

- (void)estimatedTotalData
{
    long currentRow = ((NSIndexPath *)[[tableView indexPathsForVisibleRows] lastObject]).row;

    // 25 here is the initial data count
    long estimateDataCount = 25;

    while (currentRow > estimateDataCount)
    {
        estimateDataCount+=25;
    }

    dataLimit = estimateDataCount;

    if (dataLimit == currentRow+1)
    {
        dataLimit+=25;

        if (dataLimit != estimateDataCount)
        {
            //[self requestForData]; or load necessary data
            // take not that dataLimit is the total data that must be displayed.
            NSArray *yourLocalData = @[@"1", @"2", @"3", @"4", @"5"];
            // just add more sample objects
            yourDataSource = [self setsLimitForObject: yourLocalData limit: dataLimit];
            [tableView reloadData];
        }
    }
}

- (NSArray *)setsLimitForObject:(id)object limit:(long)limit
{
    if ([object isKindOfClass:[NSArray class]])
    {
        NSMutableArray *tempArray = [object mutableCopy];

        NSRange range = NSMakeRange(0, limit);

        if (tempArray.count >= range.length)
            return [tempArray subarrayWithRange:range];

        else
        {
            NSLog(@"Out of bounds");

            return object;
        }

    } else NSLog(@"Sets Log: Cannot set limit for object %@", [object class]);

    return nil;
}

@end

2 个答案:

答案 0 :(得分:0)

尝试这种方法,这更像是分页......

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self estimatedTotalData];
}

- (void)estimatedTotalData
{
    long currentRow = ((NSIndexPath *)[[YourTableView indexPathsForVisibleRows] lastObject]).row;

    // 25 here is the initial data count
    long estimateDataCount = 25;

    while (currentRow > estimateDataCount)
    {
        estimateDataCount+=25;
    }

    dataLimit = estimateDataCount;

    if (dataLimit == currentRow+1)
    {
        dataLimit+=25;

        if (dataLimit != estimateDataCount)
        {
            //[self requestForData]; or load necessary data
            // take not that dataLimit is the total data that must be displayed.
            NSArray *yourLocalData = @[@"1", @"2", @"3", @"4", @"5"];
            // just add more sample objects
            yourDataSource = [self setsLimitForObject: yourLocalData limit: dataLimit];
            [yourTable reloadData];
        }
    }
}

更新

- (NSArray *)setsLimitForObject:(id)object limit:(long)limit
{
    if ([object isKindOfClass:[NSArray class]])
    {
        NSMutableArray *tempArray = [object mutableCopy];

        NSRange range = NSMakeRange(0, limit);

        if (tempArray.count >= range.length)
            return [tempArray subarrayWithRange:range];

        else
        {
            NSLog(@"Out of bounds");

            return object;
        }

    } else NSLog(@"Sets Log: Cannot set limit for object %@", [object class]);

    return nil;
}

答案 1 :(得分:-1)

您可以使用以下代码作为您的cellForRowAtIndexPath,

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"newFriendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = [mainArray objectAtIndex:indexPath.row];

if (indexPath.row % 5 == 0) { // this if loop will get execute at every fifth row
    // if you are loading data from API response then call API here and add next set of data into your existing array.
    // then just reload your tableview by it's name e.g. [theTableView reloadData];
}

return cell;
}

和你的numberOfRowsInSection,而不是固定的5行,只需使用如下的mainArray.count,

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
    return mainArray.count;
}