2012-12-17 14 views
12

Tôi có một bản xem trước trong ứng dụng của mình, trong đó tôi đang tải các loại ô khác nhau. Điều này là tốt và làm việc tốt. Nhưng bây giờ tôi cần phải làm việc phân trang. Tôi cần chuyển số trang cho dịch vụ dưới dạng một, hai v.v ... và tải trang đó khi trang đầu tiên kết thúc. Đối với lần đầu tiên tôi tải số lượng là 5, sau đó khi di chuyển kết thúc tôi cần phải tải tiếp theo. Tôi mới vào trang này. Ai có thể cho tôi hướng đi theo? Tôi đã cố gắng như thế này Nhưng nó sẽ trở thành một vòng lặp vô hạn 'Làm thế nào để tải tableview của tôi với một pagination?

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

    } 
} 

'

+0

Vui lòng xem điều này: HRM

+0

@Hari tôi đã kiểm tra nhưng kết thúc bằng vòng lặp vô hạn này .. – hacker

Trả lời

10

UITableView có thể hỗ trợ lướt qua nhiều dòng dữ liệu, tuy nhiên lấy một lượng lớn dữ liệu từ xa có thể làm chậm ứng dụng của bạn, sử dụng quá nhiều bộ nhớ và làm hỏng máy chủ web của bạn. Đây là tất cả lãng phí nếu người dùng không bao giờ đi để di chuyển xuống xa. Trong tập này, bạn sẽ tìm hiểu cách thực hiện phân trang UITableView tự động bằng cách sử dụng một kỹ thuật dễ dàng.

Khởi

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.beers = [NSMutableArray array]; 
    _currentPage = 0; 
} 

Issue HTTP Request cho trang hiện

- (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]; 
} 

số offset hàng

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

    if (_currentPage < _totalPages) { 
     return self.beers.count + 1; 
    } 
    return self.beers.count; 
} 

Render các tế bào

- (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]; 
    } 
} 

Tìm nạp các trang tiếp theo

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

Ví dụ này là từ NSScreencast Episode 8 - Automatic UITableViewPaging.

+0

tuyệt vời! cảm ơn! –

+12

@laxonine là từ http://nsscreencast.com/episodes/8-automatic-uitableview-paging? Nếu có, vui lòng cung cấp liên kết bên dưới bài đăng của bạn – onmyway133

0

Từ mã bạn đã đăng: bạn có chắc là indexPath.section là cần thiết không. Nếu bạn đang sử dụng phần, hãy đảm bảo rằng phương thức - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView sẽ trả về số lượng nguồn dữ liệu của bạn, không phải là số - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section.

Nếu sự cố vẫn tiếp diễn, vui lòng đăng mã phương thức ở trên.