0

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

接下来是我的问题:

  1. 如何在其中出现加载指示器的额外单元格
  2. 和加载文本如何自动获取更多文章?

此功能位于 iphone 应用程序“App Store”中,但单击加载更多项目。

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

欢迎所有示例和建议。

感谢帮助

4

3 回答 3

2

这很容易,它在最后添加一个 UITablecell 以加载更多项目。

//
//  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
于 2012-05-12T11:08:32.410 回答
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];
    }
}
于 2012-05-12T11:14:32.070 回答
0

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

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

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

cellForRowAtIndexPathtableview 委托方法中

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

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

}

这里只有简单的逻辑,其他更明智的想法是将按钮放在最后一行...希望这对您有所帮助.. :)

于 2012-05-12T10:59:06.947 回答