Ran*_*dex 4 iphone objective-c uitableview
我有自定义表格视图单元格与图像(从应用程序文档目录加载),标签,阴影等,当我滚动表格视图时,它会导致很多滞后.我怎样才能避免这些滞后?我认为可以缓存表格视图单元格,或者获取表格视图单元格的图片,但我不知道如何实现它.请帮我 :)
在cellForRowAtIndexPath中:我在if (cell == nil)块中设置数据,但仍然存在减速.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// ...configuring is here...
}
return cell;
}
Run Code Online (Sandbox Code Playgroud)
单元格中的PS图像是高分辨率,但缩小到适合...
您应该在后台线程中加载图像.如果您使用的是iOS 4+,则可以使用GCD(Grand Central Dispatch)异步加载这些内容.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"ImageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
NSString *imagepath = //get path to image here
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
dispatch_sync(dispatch_get_main_queue(), ^{
[[cell imageView] setImage:image];
[cell setNeedsLayout];
});
});
return cell;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2405 次 |
| 最近记录: |