简体   繁体   English

根据UITableView iOS中的可见单元格加载内容

[英]Load Contents based on visible cells in UITableView ios

In my app am trying to load a NSMutableArray with total 80,000 text "name" and its working perfectly but when we scroll the entire scroll get lagged(not smooth scroll). 在我的应用程序中,我试图加载一个NSMutableArray,该文本总共包含80,000个文本“名称”,并且可以正常工作,但是当我们滚动整个滚动时,它们会滞后(不平滑滚动)。

So am looking for any way to load contents to only those visible cells in UITableView (in asychronously). 因此,我正在寻找任何方式将内容仅加载到UITableView中的那些可见单元中(异步)。

This is my current code 这是我当前的代码

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    //count of section
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // return [[copyGenericDetails valueForKey:@"name"]count];    //count number of row from counting array hear cataGorry is An Array

    return [[bigArray valueForKey:@"Name"]count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:MyIdentifier];
    }


    cell.textLabel.text=[[bigArray valueForKey:@"Name"] objectAtIndex:indexPath.row];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 40;

}

Please help me with scrolling of this list. 请帮助我滚动此列表。

Assuming bigArray is what is says, ie NSArray, then this line: 假设说的是bigArray,即NSArray,则此行:

cell.textLabel.text=[[bigArray valueForKey:@"Name"] objectAtIndex:indexPath.row];

might be what's slowing you down. 可能是让您慢下来的原因。 The [bigArray valueForKey:@"Name"] causes all 80,000 entries to be scanned, and the valueForKey obtained and stored. [bigArray valueForKey:@"Name"]会扫描所有80,000个条目,并获取并存储valueForKey。 Only then do you select the correct row. 只有这样,您才能选择正确的行。 I would switch them round: 我将它们切换:

cell.textLabel.text=[[bigArray objectAtIndex:indexPath.row] valueForKey:@"Name"];

That way it's just a lookup on the 80,0000 items, and obtain the Name property for just the one item. 这样,它只是查找80,0000个项目,并仅获取一个项目的Name属性。 Likewise: 同样地:

return [[bigArray valueForKey:@"Name"]count];

can be replaced with: 可以替换为:

return [bigArray count];

if your big array is loaded means array is ready completely or no background task is running then scrolling should be fast. 如果您的大数组已加载,则意味着数组已完全就绪,或者没有后台任务在运行,那么滚动应该很快。 Check some thing extra is not effecting the scrolling like loading some thing else or any background task ? 检查一些额外的东西是否不会像加载其他东西或任何后台任务那样影响滚动?

As UITableView only loads visible cells it cannot be the tableView that is slowing you down. 由于UITableView仅加载可见单元格,因此它不会成为使您减速的tableView。 But as NSArray is not really one of the fastest access structures it might be the array that is slowing you down. 但是由于NSArray实际上并不是最快的访问结构之一,因此可能是使您减速的阵列。 Have you tried splitting your data into several smaller arrays ( lets say always 10k values into one array) and implement some logic to access the different arrays according to the indexPath.row? 您是否尝试过将数据分成几个较小的数组(比如说总是将10k值分成一个数组),并根据indexPath.row实施一些逻辑来访问不同的数组?

Oh and by the way: What device are you testing on? 哦,顺便说一句:您正在测试什么设备?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM