一次加载一个UITableViewCell

时间:2015-05-05 06:10:44

标签: ios objective-c uitableview xcode6

我正在使用以下方法将数据一次加载到我的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。数据从数组[_allTrains objectAtIndex:_currentCount]加载,我只想在最近显示5个单元格。 但是我的程序还有一个步骤,其中存在didSelectRowAtIndexPath方法。 但是在完成所有单元格加载之前,控件不会进入委托函数。我该如何解决这个问题?

4 个答案:

答案 0 :(得分:3)

这样做是怎么回事?

#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

看看我的演示项目: https://www.dropbox.com/sh/oe1t4u8zksalmdy/AAAJO7G32YUhXfw7ixBcIQXla?dl=0

希望我帮到你。 :)

答案 1 :(得分:1)

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

答案 2 :(得分:0)

更改

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

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

基本上我将waitUntilDone设置为NO

答案 3 :(得分:0)

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

相关问题