【发布时间】:2012-03-23 02:09:54
【问题描述】:
我正在将图片加载到与单元格文本对应的表格视图中,因此每个图像都不同。当用户向下滚动时,iOS 必须手动从 SSD 重新加载每个图像,因此滚动非常不稳定。如何缓存图像或防止需要重新创建表格视图单元格?其他人已经通过使用imageNamed: 解决了他们的问题,因为 iOS 会自动缓存您的图像,但我是从文档目录加载图像,而不是从应用程序包加载图像。我该怎么办?谢谢。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [issues count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSDictionary *dic = [self.issues objectAtIndex:indexPath.row];
cell.text = [dic objectForKey:@"Date"];
cell.imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/issues/%@/cover.png", documentsDirectory, [dic objectForKey:@"Directory Name"]]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 150;
}
【问题讨论】:
-
检查 UIImageLoader。它完全符合您的要求,只需添加两个文件到 xcode。它也非常容易理解,并且 repo 中有两个很棒的示例。 github.com/gngrwzrd/UIImageLoader
标签: iphone objective-c ios ipad uitableview