UITableView滚动问题
在我的应用程序中,我使用表格视图来显示图像。我连续显示 4 个图像。我能够很好地显示图像。但我的问题是,当我滚动表格视图以查看图像时,滚动会发生放置速度非常慢,而且它卡在中间。可能是什么原因。我在这里发布我的部分代码,请帮忙。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog([NSString stringWithFormat:@" numberOfRowsInSection :%d",(int)ceil([wordsInSentence count]/4.0)]);
buttonIndex=1;
return (int)ceil([wordsInSentence count]/4.0);
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell!=nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//return cell;
}
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
for (int i=0; i < 4; i++) {
UIImageView *importMediaSaveImage;
NSString *name1;
NSString *name2;
NSString *name3;
NSString *name4;
switch (i) {
case 0:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 0)){
name1= [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 0)];
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name1 ofType:@"png" inDirectory:@"Images"]];
buttonIndex=((indexPath.row * 4)+ 0)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[cell.contentView addSubview:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
NSLog(name1);
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView setHidden:YES];
}
break;
case 1:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 1)){
name2 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 1)];
NSLog(name2);
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name2 ofType:@"png" inDirectory:@"Images"]];
[cell.contentView addSubview:importMediaSaveImage];
buttonIndex=((indexPath.row * 4)+ 1)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView addSubview:importMediaSaveImage];
}
break;
case 2:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 2)){
name3 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 2)];
NSLog(name3);
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name3 ofType:@"png" inDirectory:@"Images"]];
[cell.contentView addSubview:importMediaSaveImage];
buttonIndex=((indexPath.row * 4)+ 2)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView addSubview:importMediaSaveImage];
}
break;
case 3:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 3)){
[cell.contentView addSubview:importMediaSaveImage];
name4 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 3)];
NSLog(name4);
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name4 ofType:@"png" inDirectory:@"Images"]];
buttonIndex=((indexPath.row * 4)+ 3)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView addSubview:importMediaSaveImage];
}
break;
} //switch end
} //for loop end
return cell;
}
In my app i am using a table view to display images .I am displaying 4 images in a row.I am able to display the images nicely.But my issue is that when i scroll the table view to view the images then the scrolling takes place at a very slow speed and also it gets stuck in between.What could be the reason.I am posting my part of code here ,please help.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog([NSString stringWithFormat:@" numberOfRowsInSection :%d",(int)ceil([wordsInSentence count]/4.0)]);
buttonIndex=1;
return (int)ceil([wordsInSentence count]/4.0);
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell!=nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//return cell;
}
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
for (int i=0; i < 4; i++) {
UIImageView *importMediaSaveImage;
NSString *name1;
NSString *name2;
NSString *name3;
NSString *name4;
switch (i) {
case 0:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 0)){
name1= [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 0)];
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name1 ofType:@"png" inDirectory:@"Images"]];
buttonIndex=((indexPath.row * 4)+ 0)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[cell.contentView addSubview:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
NSLog(name1);
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView setHidden:YES];
}
break;
case 1:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 1)){
name2 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 1)];
NSLog(name2);
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name2 ofType:@"png" inDirectory:@"Images"]];
[cell.contentView addSubview:importMediaSaveImage];
buttonIndex=((indexPath.row * 4)+ 1)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView addSubview:importMediaSaveImage];
}
break;
case 2:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 2)){
name3 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 2)];
NSLog(name3);
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name3 ofType:@"png" inDirectory:@"Images"]];
[cell.contentView addSubview:importMediaSaveImage];
buttonIndex=((indexPath.row * 4)+ 2)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView addSubview:importMediaSaveImage];
}
break;
case 3:
importMediaSaveImage = [[[UIImageView alloc] initWithFrame:CGRectMake(25+90*(i%4), 6, 85, 125)] autorelease] ;
if([wordsInSentence count]>((indexPath.row * 4)+ 3)){
[cell.contentView addSubview:importMediaSaveImage];
name4 = [wordsInSentence objectAtIndex:((indexPath.row * 4)+ 3)];
NSLog(name4);
importMediaSaveImage.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name4 ofType:@"png" inDirectory:@"Images"]];
buttonIndex=((indexPath.row * 4)+ 3)+1;
[self showImage:importMediaSaveImage];
[imageViewArray addObject:importMediaSaveImage];
[imageCollection addObject:importMediaSaveImage];
} else {
importMediaSaveImage.image=[UIImage imageNamed:@"White.png"];
//importMediaSaveImage.image = nil;
[cell.contentView addSubview:importMediaSaveImage];
}
break;
} //switch end
} //for loop end
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的最大问题是您正在 -cellForRowAtIndexPath 方法内进行所有渲染。这对于文本和快速迭代的数据来说效果很好,但对于从网络加载的图像或数据来说,效率非常低。
请记住,对于屏幕上可见的每个单元格,都会调用此方法一次。因此,如果您正在进行大量处理器密集型操作,则会导致滚动滞后或崩溃。
您应该阅读这篇关于快速滚动的文章由因 Tweetie 而闻名的 Loren Brichter 撰写。它解释了如何实现快速滚动出包含大量信息的表格视图。对于 iOS 4 来说,它有点过时了,因为一些方法已经改变,但它足够接近,可以让你朝着正确的方向前进。
The biggest problem you have is that you are doing all of the rendering inside the -cellForRowAtIndexPath method. This works fine for text and quickly iterated data, but for images or data loaded from the web it is terribly inefficient.
Keep in mind, this method is called once for each cell visible on the screen. So if you have a lot of processor intensive stuff going on, it is going to make scrolling lag or crash.
You should read this article on Fast Scrolling written by Loren Brichter of Tweetie fame. It explains how to achieve fast scrolling out of a tableview with lots of information. It is a little dated for iOS 4 as some of the methods have changed, but it close enough to get you going in the right direction.