异步TableViewCell数据 [英] Asynchronous TableViewCell Data

查看:151
本文介绍了异步TableViewCell数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义TableViewCell,我从数据库中返回有我需要在它的数据的UserObject异步函数获取数据。我的问题是,的cellForRowAtIndexPath 完成异步块之前返回小区。我要如何解决这个问题?

   - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    静态的NSString * simpleTableIdentifier = @ImageWorkoutCellCollapsed    ImageWorkoutCellCollapsed *电池=(ImageWorkoutCellCollapsed *)的tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];    如果(细胞==零)
    {
        NSArray的*笔尖= [[一个NSBundle mainBundle] loadNibNamed:@ImageWorkoutCellCollapsed老板:自我选择:无];
        细胞= [笔尖objectAtIndex:0];
    }    WorkoutObject *锻炼= [[WorkoutObject的alloc]初始化];
    锻炼= [appDelegate.workoutObjectsArray objectAtIndex:indexPath.row];    cell.workoutTitle.text = workout.workoutTitle;
    cell.workoutViewCount.text = workout.workoutDescription;    __block UserObject * cellUserObject = [[UserObject的alloc]初始化];
    [dbPointer getUserObjectFromDB:workout.workoutOwnerID完成:^(UserObject *结果)
    {
        cellUserObject =结果;
    }];    cell.userName.text = cellUserObject.username;
    返回细胞;
}


解决方案

您应该确保的cellForRowAtIndexPath不被调用之前是可用的数据。
你的行数的计数,必须设置此数为N,但你还没有取N个数据项。
直到您已经获取所有的数据不要设置此号码。

由于数据到达不断更新的行数并刷新表视图。

返回与占位数据的单元格。然后一次用于小区中的实际数据是可用的更新的内容和刷新表

以上所有的解决方案都涉及移动数据取出来的cellForRowAtIndexPath的。但是的 IFF 的获取数据快,因此不会减慢表的绘制的电话,你需要转换为异步同步抓取。但它不是好的设计视图控制器组件直接访问数据库,而是应该去一个模型,该模型应该抽象掉的实现细节数据是在数据库中。

I have a custom TableViewCell that I am getting data from a database in an asynchronous function that returns a UserObject that has the data I need in it. My problem is that the cellForRowAtIndexPath is returning the cell before that Asynchronous block is completed. How do i solve this problem?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *simpleTableIdentifier = @"ImageWorkoutCellCollapsed";

    ImageWorkoutCellCollapsed *cell = (ImageWorkoutCellCollapsed *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if(cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ImageWorkoutCellCollapsed" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    WorkoutObject *workout = [[WorkoutObject alloc]init];
    workout = [appDelegate.workoutObjectsArray objectAtIndex:indexPath.row];

    cell.workoutTitle.text = workout.workoutTitle;
    cell.workoutViewCount.text = workout.workoutDescription;

    __block UserObject *cellUserObject = [[UserObject alloc]init];


    [dbPointer getUserObjectFromDB:workout.workoutOwnerID completion:^(UserObject *result)
    {
        cellUserObject = result;
    }];

    cell.userName.text = cellUserObject.username;
    return cell;
}

解决方案

You should make sure cellForRowAtIndexPath is not being called before the data is available. You have a count of the number of rows, you must be setting this count to N but you haven't yet fetched N data items. Don't set this number until you have fetched all the data.

OR

As data arrives continually update the number or rows and refresh the table view.

OR

Return the cell with placeholder data. Then once the actual data for the cell is available update the content and refresh the table.

OR

All of the above solutions involves moving the data fetch out of cellForRowAtIndexPath. However IFF the call to fetch the data is quick and thus won't slow down the drawing of the table, you need to convert the fetch from being asynchronous to synchronous. But it is not good design for a view controller component to directly access a db, instead it should be going to a model and the model should abstract away the implementation detail that the data is in a database.

这篇关于异步TableViewCell数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆