在我的应用程序中,我试图加载一个 NSMutableArray,其中包含总共 80,000 个文本“名称”,并且它工作正常,但是当我们滚动整个滚动条时会出现滞后(滚动不流畅)。
所以我正在寻找任何方法将内容仅加载到 UITableView 中的那些可见单元格(异步)。
这是我当前的代码
- (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;
}
请帮助我滚动此列表。
最佳答案
假设 bigArray 就是所说的,即 NSArray,那么这一行:
cell.textLabel.text=[[bigArray valueForKey:@"Name"] objectAtIndex:indexPath.row];
可能是什么让你慢下来。 [bigArray valueForKey:@"Name"]
导致扫描所有 80,000 个条目,并获取并存储 valueForKey。只有这样您才能选择正确的行。我会把它们调过来:
cell.textLabel.text=[[bigArray objectAtIndex:indexPath.row] valueForKey:@"Name"];
这样它只是对 80,0000 项的查找,并只获取一项的 Name 属性。同样:
return [[bigArray valueForKey:@"Name"]count];
可以替换为:
return [bigArray count];
关于ios - 基于 UITableView ios 中的可见单元格加载内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26774266/