Ios 每次加载一个UITableViewCell

Ios 每次加载一个UITableViewCell,ios,objective-c,uitableview,xcode6,Ios,Objective C,Uitableview,Xcode6,我正在按以下方法一次一个地将数据加载到UITableViewCell中 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ _anIterationCounter++; if (_anIterationCounter==_currentCount) { if ((_currentCount

我正在按以下方法一次一个地将数据加载到UITableViewCell中

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
_anIterationCounter++;

if (_anIterationCounter==_currentCount) {
    if ((_currentCount+1)<=[_allTrains count]&&(_currentCount+1)<=5) {
        _currentCount++;
        _anIterationCounter=0;

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
        dispatch_async(queue, ^{
           [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; //call apis here
        });

    }
}
_iterationCount变量在开始时初始化为0。数据是从数组[\u allTrains objectAtIndex:\u currentCount]加载的,我只想显示5个单元格atmost。 但我的程序还有一个步骤,其中存在didSelectRowAtIndexPath方法。 但在所有单元格加载完成之前,控件不会进入委托函数。我将如何解决这个问题

更改

[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];

基本上,我将waitUntilDone设置为NO.

更改

[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];


基本上我把waitUntilDone设置为NO.

这样做怎么样

#import "TableViewController.h"

@interface TableViewController ()

@property (strong, nonatomic) NSArray *regularData;
@property (strong, nonatomic) NSMutableArray *lazyLoadedData;

@property (strong, nonatomic) NSTimer *insertTimer;

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.tableFooterView = [[UIView alloc] init];

    self.regularData = @[@"value1",
                     @"value2",
                     @"value3",
                     @"value4",
                     @"value5",
                     @"value6",
                     @"value7",
                     @"value8",
                     @"value9"];

    self.lazyLoadedData = [@[] mutableCopy];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    self.insertTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(insertRow) userInfo:nil repeats:YES];
}

- (void)insertRow {
    if (self.lazyLoadedData.count == 5) {
        // maximum reached
        [self.insertTimer invalidate];
        self.insertTimer = nil;
        return;
    }

    [self.lazyLoadedData addObject:self.regularData[self.lazyLoadedData.count]];
    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.lazyLoadedData.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.lazyLoadedData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.lazyLoadedData[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

@end
或者看看我的演示项目:


希望我没弄错

这样做怎么样

#import "TableViewController.h"

@interface TableViewController ()

@property (strong, nonatomic) NSArray *regularData;
@property (strong, nonatomic) NSMutableArray *lazyLoadedData;

@property (strong, nonatomic) NSTimer *insertTimer;

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.tableFooterView = [[UIView alloc] init];

    self.regularData = @[@"value1",
                     @"value2",
                     @"value3",
                     @"value4",
                     @"value5",
                     @"value6",
                     @"value7",
                     @"value8",
                     @"value9"];

    self.lazyLoadedData = [@[] mutableCopy];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    self.insertTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(insertRow) userInfo:nil repeats:YES];
}

- (void)insertRow {
    if (self.lazyLoadedData.count == 5) {
        // maximum reached
        [self.insertTimer invalidate];
        self.insertTimer = nil;
        return;
    }

    [self.lazyLoadedData addObject:self.regularData[self.lazyLoadedData.count]];
    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.lazyLoadedData.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.lazyLoadedData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.lazyLoadedData[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

@end
或者看看我的演示项目:


希望我没弄错

使用UISCrollView并创建单元格对象添加到scrollview中,然后一次加载所有单元格。

使用UISCrollView并创建单元格对象添加到scrollview中,然后一次加载所有单元格。

而不是每次重新加载表,使用tableView的InsertatindExaths:方法逐个插入单元格。

不要每次重新加载表格,而是使用tableView的InsertatindExaths:方法逐个插入单元格。

这不起作用。尽管我真的希望这样做。它使sense@SidharthJDev为什么每次调用willDisplayCell时都要重新加载表?这有点奏效。在加载tableview时,我多次单击,但什么也没发生。但是一旦加载完成,所有的alertview都会依次出现。当我点击一个单元格时,alertview就会出现shown@SidharthJDev我想与其重新装填整张桌子,您应该使用reload row at index path:index path来重新加载当前行每个单元格都有一个必须用API中的数据填充的数据。API在willDisplayCell内单独调用。每个响应大约需要5秒钟。如果我不这样做,加载不起作用的数据需要20多秒,尽管我真的希望它能起作用。它使sense@SidharthJDev为什么每次调用willDisplayCell时都要重新加载表?这有点奏效。在加载tableview时,我多次单击,但什么也没发生。但是一旦加载完成,所有的alertview都会依次出现。当我点击一个单元格时,alertview就会出现shown@SidharthJDev我想与其重新装填整张桌子,您应该使用reload row at index path:index path来重新加载当前行每个单元格都有一个必须用API中的数据填充的数据。API在willDisplayCell内单独调用。每个响应大约需要5秒钟。如果我不这样做,加载数据需要20秒以上。你的答案不是我们在这个网站上想要的格式。一旦链接消失,你的贡献就一文不值了。你的答案不是我们希望他们出现在这个网站上的形式。一旦链接消失,你的贡献就一文不值了。我不明白你想要实现什么;你为什么不把你的细胞装进cellForRow?我不明白你想达到什么目的;你为什么不把你的手机放进cellForRow?