UITableView load more when scrolling to bottom like Facebook application

后端 未结 18 2118

I am developing an application that uses SQLite. I want to show a list of users (UITableView) using a paginating mechanism. Could any one please tell me how to load more dat

18条回答
  •  [愿得一人]
    2020-11-27 09:37

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        if (news.count == 0) {
            return 0;
        } else {
            return news.count +  1 ;
        }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        @try {
    
            uint position = (uint) (indexPath.row);
            NSUInteger row = [indexPath row];
            NSUInteger count = [news count];
    
            //show Load More
            if (row == count) {
                UITableViewCell *cell = nil;
    
                static NSString *LoadMoreId = @"LoadMore";
                cell = [tableView dequeueReusableCellWithIdentifier:LoadMoreId];
                if (cell == nil) {
                    cell = [[UITableViewCell alloc]
                            initWithStyle:UITableViewCellStyleDefault
                          reuseIdentifier:LoadMoreId];
                }
                if (!hasMoreLoad) {
                    cell.hidden = true;
                } else {
    
                    cell.textLabel.text = @"Load more items...";
                    cell.textLabel.textColor = [UIColor blueColor];
                    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
                    NSLog(@"Load more");
                    if (!isMoreLoaded) {
                        isMoreLoaded = true;
                        [self performSelector:@selector(loadMoreNews) withObject:nil afterDelay:0.1];
                    }
                }
    
                return cell;
    
            } else {
                NewsRow *cell = nil;
    
                NewsObject *newsObject = news[position];
                static NSString *CellIdentifier = @"NewsRow";
                cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
                if (cell == nil) {
                    // Load the top-level objects from the custom cell XIB.
                    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
                    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
                    cell = topLevelObjects[0];
                    // Configure the cell...
    
                }
    
                cell.title.text = newsObject.title;             
                return cell;
            }
    
        }
        @catch (NSException *exception) {
            NSLog(@"Exception occurred: %@, %@", exception, [exception userInfo]);
        }
        return nil;
    }
    

    very good explanation on this post.

    http://useyourloaf.com/blog/2010/10/02/dynamically-loading-new-rows-into-a-table.html

    simple you have to add last row and hide it and when table row hit last row than show the row and load more items.

提交回复
热议问题