在异步重载数据期间滚动时,为什么UITableView失去了流动性? [英] Why does my UITableView lose it's fluidity when I scroll during an asynchronous reload data?

查看:68
本文介绍了在异步重载数据期间滚动时,为什么UITableView失去了流动性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题...我正在异步调用要使用的服务,该服务将搜索结果返回给我.完成后,它将重新加载我的表,并且异步完成.问题是,当我尝试在异步调用完成之前在表格视图上滚动时,表格会失去所有滚动视图所具有的那种流畅的感觉.我的意思是,当您放开滚动条,而不是继续前进并缓慢停止时,它会在您放开后立即停止.恢复流畅感的唯一方法是一直滚动到UITableView的顶部或底部以及一点过去,然后就可以了.

Here is my problem... I'm making an asynchronous call to the service I'm working with, which returns search results back to me. When that's done, it reloads my table, and the async is done. The problem is, when I try scrolling on my table view before the async call is done, the table loses that fluid feeling that all scroll views have. What I mean by this is that when you let go of the scroll, rather than keeping going and slowly stopping, it just halts as soon as you let go. The only way to restore the fluid feeling is to scroll all the way to the top or bottom of the UITableView and a little past, then it's fine.

这是我的异步调用代码:

Here is my code for the async call:

__block NSMutableArray *appendMoreObjects = [[NSMutableArray alloc] init];

    dispatch_async( dispatch_get_main_queue(), ^{

        self.activity.hidden = NO;

        appendMoreObjects = [service search:self.query from:((moreCount * 8)) until:((moreCount * 8) + 8)];

        if( appendMoreObjects == nil )
        {
            //alert view
        }
        else
        {
            [self.allSearchResults addObjectsFromArray:appendMoreObjects];

            [self updateSearchArray];

            self.moreCount += 1;
        }

        self.activity.hidden = YES;
    });

此外,请记住,我正在使用自定义UITableViewCell.如果需要其他任何信息,请提出要求,我会做的.预先谢谢你.

Also, keep in mind that I'm using a custom UITableViewCell. If any other information is needed, please request it and I'll see what I can do. Thank you in advance.

[self updateSearchArray]包含[tableView reloadData]

[self updateSearchArray] contains the [tableView reloadData]

我正在运行4.3.1(8G4)的iPod Touch上进行测试

I'm testing on an iPod Touch running 4.3.1 (8G4)

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
customCell * cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];

if( cell == nil )
{
    cell = [[[customCell alloc] initWithFrame:CGRectZero] autorelease];
}

if( indexPath.row == [self.searchResults count] && [self.searchResults count] > 0)
{

    UITableViewCell *moreCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
    moreCell.textLabel.text = @"More";
    moreCell.textLabel.textAlignment = UITextAlignmentCenter;

    return moreCell;
}

if( indexPath.row < [self.searchResults count] )
{
    [cell formatAndInsertData:[self.searchResults objectAtIndex:indexPath.row]];
}

return cell;
}

解决方案:

我实际上最终要做的是锁定表和所有UI元素,直到调用完成.也许不是最好的解决方案,但仍然有效.

What I actually ended up doing was locking down the table and all UI elements until the call was done. Maybe not the best solution, but it worked none the less.

推荐答案

调用[tableView reloadData]将重新加载整个ENTIRE表,这就是看到不稳定的UITableView的原因.它会强制刷新所有可见的单元格,因此在滚动时,可见的单元格会刷新并重新绘制自身.您需要重新评估更新UITableView的策略,因为有数据可以避免这种情况.您应该更新可见的单个单元格,这些单元格具有与您的更新相关的数据.

Calling [tableView reloadData] will reload your ENTIRE table, which is the reason you are seeing the choppy UITableView. It forces all visible cells to refresh, so as you are scrolling, the visible cells are refreshing and repainting themselves. You'll need to reevaluate your strategy for updating the UITableView as you have data coming in to avoid this. You should update individual cells that could be visible that have data that pertain to your updates.

为此,您将需要使用以下方法:

To do this, you will need to use the following methods:

– insertRowsAtIndexPaths:withRowAnimation:

– deleteRowsAtIndexPaths:withRowAnimation:

– reloadRowsAtIndexPaths:withRowAnimation:

这些的组合将使您在接收数据时更新表(添加/删除行),而不必强制刷新整个表.

The combination of these will allow you to update your table (add/remove rows), as you receive data, and not force the refresh of the entire table.

这篇关于在异步重载数据期间滚动时,为什么UITableView失去了流动性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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