开发者

Creating a thumbnail for UITableViewCell imageView

开发者 https://www.devze.com 2023-03-22 02:19 出处:网络
I was looking how to resize images for the UITableViewCells so they are all the same size.I came across this post UITableViewCell's imageView fit to 40x40, and added the method to create a thumbna

I was looking how to resize images for the UITableViewCells so they are all the same size. I came across this post UITableViewCell's imageView fit to 40x40, and added the method to create a thumbnail as a category. It works, but seems slower now and I'm wondering if I'm leaving something out or using the category incorrectly. Currently, in my cellForRowAtIndexPath method, that is where I create a new image, and use this category to resize it. So I'm guessing this is not the way to do it since now it's doing this drawing each time it wants to display in the table? Do I need to create the array of thumbnails per row and use that instead of the original images? If so, how would I keep track of both arrays (one for text, one for images), in case something gets reordered. Thanks!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *PopoverIdentifier = @"PopoverIdentifier";
    UITableViewCell开发者_如何学JAVA *cell = [tableView dequeueReusableCellWithIdentifier:PopoverIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PopoverIdentifier] autorelease];
    }
    NSUInteger row = [indexPath row];
    GTest *gt = [_gtList objectAtIndex:row];
    cell.textLabel.text = gt.Name;
    UIImage *cellImage = [UIImage imageNamed:gt.ImageFile];
    cellImage = [UIImage scale:cellImage toSize:CGSizeMake(50, 50)];
    cell.imageView.image = cellImage;
    return cell;

}


you could create an array of dictionary objects which hold the image and the text data:

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:text,@"text",image,@"image"nil];
[array addObject:dict];

and in your cellForRowAtIndexPath:

NSDictionary* dict = [array objectAtIndex:i];
UIImage* img = [dict objectForKey:@"image"];
NSString* txt = [dict objectForKey:@"text"];
0

精彩评论

暂无评论...
验证码 换一张
取 消