在TableView中自动加载更多文章

时间:2012-05-12 10:44:13

标签: iphone objective-c xcode uitableview

我有TableView,在这个TableView中我正在打印文章。现在用户必须单击导航栏项目进行更新(通过json从web下载更多文章)。我想,并且我认为当用户滚动到底部时自动显示加载单元格并开始加载或从网络上获取更多文章时更好。

我的问题是下一步:

  1. 如何将额外的单元格放入装载指示器
  2. 并加载文字如何自动获取更多文章?
  3. 此功能在iphone应用程序“App Store”中,但点击加载更多项目。

    也许最好把按钮加载更多文章?

    欢迎所有的例子和建议。

    感谢您的帮助

3 个答案:

答案 0 :(得分:2)

这个很容易在最后添加一个表格单元格以加载更多项目。

//
//  TableViewController.m
//  PartialTable
//
//  Created by Abizer Nasir on 07/07/2011.
//

#import "TableViewController.h"

#define kNumberOfItemsToAdd 8

@implementation TableViewController

@synthesize items;

// Mark: -
// Mark: Set up and tear down

- (id)init  {
    // New designated initialiser
    if (!(self = [super initWithStyle:UITableViewStyleGrouped])) {
        return nil; // Bail!
    }
    numberOfItemsToDisplay = kNumberOfItemsToAdd; // Show 10 items at startup
    return self;
}


- (id)initWithStyle:(UITableViewStyle)style {
    // Call out to the new designated initialiser
    return [self init];
}

- (void)dealloc {
    [items release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (numberOfItemsToDisplay == [items 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 {
    static NSString *CellIdentifier = @"ItemCell";

    // If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
    // otherwise, replace it with a button cell.

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    if (indexPath.section == 0) {            
        cell.textLabel.text = [items objectAtIndex:indexPath.row];
        cell.textLabel.textAlignment = UITextAlignmentLeft;        
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:17.f];

    } else {
        cell.textLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Next %d items", @"The text to display to load more content"), kNumberOfItemsToAdd];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.textColor = [UIColor colorWithRed:0.196f green:0.3098f blue:0.52f alpha:1.f];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14.f];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        NSUInteger i, totalNumberOfItems = [items count];        
        NSUInteger newNumberOfItemsToDisplay = MIN(totalNumberOfItems, numberOfItemsToDisplay + kNumberOfItemsToAdd);
        NSMutableArray *indexPaths = [[NSMutableArray alloc] init];        

        for (i=numberOfItemsToDisplay; i<newNumberOfItemsToDisplay; i++) {
            [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
        }        

        numberOfItemsToDisplay = newNumberOfItemsToDisplay;                

        [tableView beginUpdates];
        [tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
        [indexPaths release];                
        if (numberOfItemsToDisplay == totalNumberOfItems) {
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationTop];
        }        
        [tableView endUpdates];
        // Scroll the cell to the top of the table
        if (newNumberOfItemsToDisplay < totalNumberOfItems) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200000000), dispatch_get_main_queue(), ^(void){
                [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
            });
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        } else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200000000), dispatch_get_main_queue(), ^(void){
                [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:totalNumberOfItems-1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            });
        }

    }    
}

@end

答案 1 :(得分:2)

Q1:再添加1个单元格以加载更多状态:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return cellsCount <= 0 ? 0 : cellsCount + 1;
}

并创建加载更多单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == cellsCount) {
        if (loadMoreCell == nil) {
            self.loadMoreCell = [[LoadMoreTableCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                         reuseIdentifier:@"LoadMoreCellIdentifier"];
        }
        return loadMoreCell;
    }
    ...
}

您可以根据需要自定义LoadMoreTableCell

Q2:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    float offset = (scrollView.contentOffset.y - (scrollView.contentSize.height - scrollView.frame.size.height));
    if (offset >= 0 && offset <= 5) {
        [self loadMoreData];
    }
}

答案 2 :(得分:0)

我也在我的应用中喜欢这个东西, 在这里你可以使用AsyncImage类,这对于在后台下载带有url的图像很有用,所以你的tableview滚动顺畅..

这里有两个链接希望这有助于您在后台下载图片... 1.https://github.com/Koolistov/Image-Cache 2.https://github.com/rs/SDWebImage

第二件事你想在底部下载到另一个数据和单元格然后在这里如果你使用1个条件然后你喜欢的工作...

cellForRowAtIndexPath tableview委托方法

if(indexPath.row==[yourArray count]-1){

   .....do somthing which you like.....(Reload table with your json or another data)

}

这里只有简单的逻辑,其他明智的更好的想法就是把按钮放在最后一排...... 希望这能帮到你.. :)

相关问题