簡短的回答:一旦你有圖像,然後image.size.height應該給你你想要的。然而,heightForRowAtIndexPath:在cellForRowAtIndexPath:之前被調用。所以你可能會想要實現一些類型的異步圖像加載器。查看蘋果示例LazyTableImages以獲得完整的實現。
長答案:我做了幾個捷徑來回答你的問題,而不寫1000行代碼,但是這應該證明這個想法。重要的位是cellForRowAtIndexPath:和heightForRowAtIndexPath :.
RootViewController.m
#import "RootViewController.h"
@interface RootViewController()
- (void)loadImagesForOnscreenRows;
- (void)loadImageForIndexPath:(NSIndexPath *)indexPath;
@end
@implementation RootViewController
static NSMutableDictionary *images;
static NSDictionary *pathAndName;
#pragma mark -
#pragma mark NSObject
- (void)dealloc {
[images release];
[pathAndName release];
[super dealloc];
}
#pragma mark UIViewController
- (void)viewDidLoad {
// image paths from WikiMedia
pathAndName = [[NSDictionary alloc] initWithObjectsAndKeys:
@"http://upload.wikimedia.org/wikipedia/commons/8/89/Crystal_Clear_app_virus_detected_2.png", @"Big Virus",
@"http://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Crystal_Clear_app_virus_detected.png/64px-Crystal_Clear_app_virus_detected.png", @"Little Virus",
nil];
images = [[NSMutableDictionary alloc] initWithCapacity:[pathAndName allKeys].count];
}
- (void)viewDidUnload {
[images release];
images = nil;
[pathAndName release];
pathAndName = nil;
}
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate) {
[self loadImagesForOnscreenRows];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self loadImagesForOnscreenRows];
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [pathAndName allKeys].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// typical cell creation
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// if the image is downloaded, then set the cell image.
NSString *key = [[pathAndName allKeys] objectAtIndex:indexPath.row];
UIImage *image = [images objectForKey:key];
if (image) {
[cell.imageView setImage:image];
}
[cell.textLabel setText:key];
return cell;
}
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[pathAndName allKeys] objectAtIndex:indexPath.row];
UIImage *image = [images objectForKey:key];
if (image) {
return image.size.height;
}
// if the image is not yet downloaded, kick off downloading it and return a default row height.
[self performSelector:@selector(loadImageForIndexPath:) withObject:indexPath afterDelay:0.1f];
return tableView.rowHeight;
}
#pragma mark -
#pragma mark RootViewController
#pragma mark Private Extension
- (void)loadImagesForOnscreenRows {
NSArray *visiblePaths = [self.tableView indexPathsForVisibleRows];
for (NSIndexPath *indexPath in visiblePaths) {
[self loadImageForIndexPath:indexPath];
}
}
// This is a big shortcut from the Apple sample, LazyTableImages.
- (void)loadImageForIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[pathAndName allKeys] objectAtIndex:indexPath.row];
if ([images objectForKey:key]) return;
NSString *path = [pathAndName objectForKey:key];
NSError *error = nil;
NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:path]
options:NSUncachedRead
error: &error];
if (error) return;
UIImage *image = [[UIImage alloc] initWithData:receivedData];
[images setObject:image forKey:key];
[image release];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
@end
非常感謝這一點 - 我發現LazyTableImages,它有很大的幫助 – daidai 2010-11-08 22:13:44