gpt4 book ai didi

iphone - 当单元格具有 UIImageView subview 时,UITableView 会出现不稳定的滚动

转载 作者:行者123 更新时间:2023-12-03 19:10:28 25 4
gpt4 key购买 nike

这一天的大部分时间里,这都让我发疯。

我有一个带有 UIImageViews 的 UITableView。这些 ImageView 在 tableview 的 cellForRow 函数中加载本地保存的 PNG 文件,这工作得很好,只是当其中包含图像的单元格滚动到视野中时,tableview 将停止滚动几分之一秒。我已经在 StackOverflow 和 google 上寻找答案,但没有找到答案 - 因此我们将不胜感激任何帮助。

这是我的 CellForRow 函数的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];



if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

if([currSection isEqualToString:@"composer"]){

MySlide *s = [slidesArray objectAtIndex:indexPath.row];

cell.textLabel.hidden = YES;
UIImageView *whiteView = [[UIImageView alloc] initWithFrame:CGRectMake((projectsTable.frame.size.width/2)-150, 4, 204.8, 153.6)];

if([s.slideImage isEqualToString:@""] || s.slideImage == nil){
//no custom image in this cell - go with default background image

whiteView.image = [UIImage imageNamed:@"cellback2.png"];
whiteView.backgroundColor = [UIColor whiteColor];
}else{
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

NSData *data = [[NSData alloc] initWithContentsOfFile:s.slideImage];

UIImage *im = [[UIImage alloc] initWithData:data];


whiteView.image = im;

whiteView.image = [self imageWithImage:whiteView.image CovertToSize:CGSizeMake(204.8,153.6)];
whiteView.backgroundColor = [UIColor whiteColor];

}


[cell.contentView addSubview:whiteView];

[whiteView release];



cell.accessoryType = UITableViewCellAccessoryNone;

}


return cell;
}

最佳答案

需要进行一些更改,首先应在生成单元格时添加 UIImageView,而不是每次 tableView:cellForRowAtIndexPath: 时添加命中(正如@Vishy 建议的那样)。其次,您应该缓存从文档目录加载的图像([UIImage imageNamed:] 会自动为捆绑资源执行此操作)。

@interface MyViewController () {

NSMutableDictionary *_imageCache;
}

@end


@implementation MyViewController

- (void)viewDidLoad {
[super viewDidLoad];

// other viewDidLoad stuff...

_imageCache = [[NSMutableDictionary alloc] init];
}

- (void)viewDidUnload {
[super viewDidUnload];

// other viewDidUnload stuff...

[_imageCache release];
_imageCache = nil;
}

- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

UIImageView *whiteView = [[UIImageView alloc] initWithFrame:CGRectMake((projectsTable.frame.size.width/2)-150, 4, 204.8, 153.6)];
whiteView.tag = 111;
whiteView.backgroundColor = [UIColor whiteColor];

[cell.contentView addSubview:whiteView];

[whiteView release];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.hidden = YES;
}

UIImageView* iView = (UIImageView*) [cell.contentView viewWithTag:111];


if([currSection isEqualToString:@"composer"]) {

MySlide *s = [slidesArray objectAtIndex:indexPath.row];

if([s.slideImage isEqualToString:@""] || s.slideImage == nil) {

//no custom image in this cell - go with default background image
iView.image = [UIImage imageNamed:@"cellback2.png"];
}
else {

cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

// use the image path as the cache key
UIImage *theImage = [_imageCache objectForKey:s.slideImage];
if (theImage == nil) {

// load the image and save into the cache
theImage = [UIImage imageWithContentsOfFile:s.slideImage];
theImage = [self imageWithImage:theImage CovertToSize:CGSizeMake(204.8, 153.6)];

[_imageCache setObject:theImage forKey:s.slideImage];
}

iView.image = theImage;
}
}
}

@end

作为一般规则,tableView:cellForRowAtIndexPath: 是一种需要摆脱快速的方法,因此应尽可能避免从磁盘加载图像。

关于iphone - 当单元格具有 UIImageView subview 时,UITableView 会出现不稳定的滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9159519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com