我正在將圖片加載到與單元格文本對應的表格視圖中,因此每個圖像都不相同。隨着用戶向下滾動,iOS必須手動重新加載SSD中的每個圖像,因此滾動非常不連貫。如何緩存圖像或防止需要重新創建表格視圖單元格?其他人通過使用imageNamed:
解決了他們的問題,因爲iOS會自動緩存您的圖像,但我從文檔目錄加載圖像,而不是應用程序包。我該怎麼辦?謝謝。在UITableView中緩存圖像
- (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。這也是非常容易理解的,回購中有兩個很棒的樣本。 https://github.com/gngrwzrd/UIImageLoader – gngrwzrd 2015-12-25 20:36:24