如何用分页加载我的tableview?

hac*_*ker 15 iphone objective-c uitableview ipad ios

我的应用程序中有一个tableview,我正在加载不同类型的单元格.这很好,运作良好.但现在我需要做分页.我需要将页码作为一,二等传递给服务,并在第一页结束时加载.对于我加载的第一个计数是5,之后当滚动结束时我需要加载下一个.我是这个分页的新手.任何人都可以给我指示方向吗?我试过这样但它会成为一个无限循环`

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *) cell     forRowAtIndexPath:(NSIndexPath *)indexPath
{
    int count1 = [array count];
    NSLog(@"%d",count1);
     NSLog(@"%d",indexPath.section);
    NSLog(@"%d",max);
    NSLog(@"%d",min);

    if(indexPath.section == count1 - 1) // If going to display last row available in your source
    {

        NSLog(@"%d",max);
          NSLog(@"%d",min);


        if(min <= max) 
        {



            [self getmessages:min];
        }
        else
        {
            self.mtableview.tableFooterView = nil; //You can add an activity indicator in tableview's footer in viewDidLoad to show a loading status to user.
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

`

lax*_*ine 10

UITableView可以支持浏览多行数据,但是获取大量远程数据可能会降低应用程序速度,耗尽太多内存,并使Web服务器陷入困境.如果用户不想向下滚动那么远,那就太浪费了.在本集中,您将学习如何使用简单的技术执行自动UITableView分页.

初始化

- (void)viewDidLoad {
    [super viewDidLoad];

    self.beers = [NSMutableArray array];
    _currentPage = 0;
}
Run Code Online (Sandbox Code Playgroud)

发出当前页面的HTTP请求

- (void)fetchBeers {
    NSString *urlString = [NSString 
      stringWithFormat:@"http://localhost:3000/beers.json?page=%d", 
      _currentPage];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation = 
      [[AFJSONRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:
      ^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"responseObject %@", responseObject);

        _totalPages = [[responseObject 
          objectForKey:@"total_pages"] intValue];

        for (id beerDictionary in [responseObject 
                                    objectForKey:@"beers"]) {
            Beer *beer = [[Beer alloc] 
              initWithDictionary:beerDictionary];
            if (![self.beers containsObject:beer]) {
                [self.beers addObject:beer];
            }

            [beer release];
        }

        [self.tableView reloadData];

    } failure:
      ^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", [error localizedDescription]);
        [[[[UIAlertView alloc] 
          initWithTitle:@"Error fetching beers!"
                message:@"Please try again later"
               delegate:nil
          cancelButtonTitle:@"OK"
          otherButtonTitles:nil] autorelease] show];
    }];

    [operation start];
    [operation release];
}
Run Code Online (Sandbox Code Playgroud)

偏移行数

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
    if (_currentPage == 0) {
        return 1;
    }

    if (_currentPage < _totalPages) {
        return self.beers.count + 1;
    }
    return self.beers.count;
}
Run Code Online (Sandbox Code Playgroud)

渲染单元格

- (UITableViewCell *)beerCellForIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [self.tableView 
dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] 
            initWithStyle:UITableViewCellStyleSubtitle
          reuseIdentifier:cellIdentifier] autorelease];
    }

    Beer *beer = [self.beers objectAtIndex:indexPath.row];
    cell.textLabel.text = beer.name;
    cell.detailTextLabel.text = beer.brewery;

    return cell;
}

- (UITableViewCell *)loadingCell {
    UITableViewCell *cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault
               reuseIdentifier:nil] autorelease];

    UIActivityIndicatorView *activityIndicator = 
      [[UIActivityIndicatorView alloc] 
      initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicator.center = cell.center;
    [cell addSubview:activityIndicator];
    [activityIndicator release];

    [activityIndicator startAnimating];

    cell.tag = kLoadingCellTag;

    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < self.beers.count) {
        return [self beerCellForIndexPath:indexPath];
    } else {
        return [self loadingCell];
    }
}
Run Code Online (Sandbox Code Playgroud)

获取下一页

- (void)tableView:(UITableView *)tableView 
  willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (cell.tag == kLoadingCellTag) {
        _currentPage++;
        [self fetchBeers];
    }
}
Run Code Online (Sandbox Code Playgroud)

此示例来自NSScreencast Episode 8 - Automatic UITableViewPaging.

  • @laxonine来自http://nsscreencast.com/episodes/8-automatic-uitableview-paging?如果是,请在您的帖子下面给出链接 (12认同)