0

私はTableViewを持っており、このTableViewで記事を印刷しています。ここで、ユーザーはナビゲーションバーの項目をクリックして更新する必要があります(jsonを介してWebからさらに記事をダウンロードします)。ユーザーが一番下までスクロールすると、自動的に読み込み中のセルが表示され、Webからの記事の読み込みまたは取得を開始する方がよいと思います。

私の質問は次です:

  1. ローディングインジケーターが表示される余分なセルを配置する方法
  2. とテキストの読み込みより多くの記事を自動的に取得するにはどうすればよいですか?

この機能はiPhoneアプリ「AppStore」にありますが、クリックするとさらにアイテムが読み込まれます。

たぶん、ボタンロードをもっと多くの記事に置く方が良いですか?

すべての例と提案を歓迎します。

手伝ってくれてありがとう

4

3 に答える 3

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 に答える
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 に答える
0

私も私のアプリでこれが好きです。ここでは、AsyncImageクラスを使用できます。これは、バックグラウンドでURLを含む画像をダウンロードして、テーブルビューをスムーズにスクロールするのに役立ちます。

ここに2つのリンクがあります。これがバックグラウンドで画像をダウンロードするのに役立つことを願っています...1.https://github.com/Koolistov/Image-Cache 2.https://github.com/rs/SDWebImage

一番下にある別のデータとセルにダウンロードしたい2番目のもの、次に1つの条件を使用する場合は、その作業が好きです...

テーブルビューのcellForRowAtIndexPathデリゲートメソッド

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 に答える