Iphone 带有性能问题的UITableView
我在应用程序中使用3种类型的单元格创建UITableview(带IB)。所有的工作都很好,除了当我拖动我的tableview时,一切都变得非常缓慢。因此,如果有人能帮助我提高我的应用程序的性能,那就太棒了 我的tableview代码:Iphone 带有性能问题的UITableView,iphone,ios,performance,uitableview,Iphone,Ios,Performance,Uitableview,我在应用程序中使用3种类型的单元格创建UITableview(带IB)。所有的工作都很好,除了当我拖动我的tableview时,一切都变得非常缓慢。因此,如果有人能帮助我提高我的应用程序的性能,那就太棒了 我的tableview代码: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[myData objectForKey:@"listnews"] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 154;
}
if (indexPath.row == 1) {
return 34;
}
else return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[myTextField resignFirstResponder];
ListNewsCell *cCell = (ListNewsCell *)[myTable dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cCellIdentifier%d",indexPath.row]];
cCell = [[ListNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cCellIdentifier%d",indexPath.row]];
if (indexPath.row == 0) {
NSString * caroline = [[[myData objectForKey:@"listnews"] objectAtIndex:indexPath.row] objectForKey:@"urlimage"];
cCell.c1Image1.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",caroline]]]];
cCell.c1Image2.image = [UIImage imageNamed:@"barreNoireHub.png"];
NSString * sophie = [[[myData objectForKey:@"listnews"] objectAtIndex:indexPath.row] objectForKey:@"titre"];
NSString *itemTitle = sophie ? [sophie stringByConvertingHTMLToPlainText] : @"[No Title]";
cCell.c1Label1.text = itemTitle;
NSString * sonia = [[[myData objectForKey:@"listnews"] objectAtIndex:indexPath.row] objectForKey:@"extrait"];
NSString *itemExtrait = sonia ? [sonia stringByConvertingHTMLToPlainText] : @"[No Title]";
cCell.c1Label2.text = itemExtrait;
}
if (indexPath.row == 1) {
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 34)];
imageView.backgroundColor = [UIColor whiteColor];
imageView.image = [UIImage imageNamed:@"barreSearchHub.png"];
[cCell addSubview:imageView];
[imageView release];
UIView * insertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 34)];
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 6, 270, 25)];
myTextField.placeholder = @"Recherche";
myTextField.tag = 1;
myTextField.delegate = self;
myTextField.returnKeyType = UIReturnKeySearch;
[insertView addSubview:myTextField];
cCell.accessoryView = insertView;
}
if (indexPath.row > 1) {
cCell.c2Image1.image = [UIImage imageNamed:@"cellM.png"];
NSString * caroline = [[[myData objectForKey:@"listnews"] objectAtIndex:indexPath.row] objectForKey:@"urlimage"];
cCell.c2Image2.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",caroline]]]];
NSString * sophie = [[[myData objectForKey:@"listnews"] objectAtIndex:indexPath.row] objectForKey:@"titre"];
NSString *itemTitle = sophie ? [sophie stringByConvertingHTMLToPlainText] : @"[No Title]";
cCell.c2Label1.text = itemTitle;
NSString * sonia = [[[myData objectForKey:@"listnews"] objectAtIndex:indexPath.row] objectForKey:@"extrait"];
NSString *itemExtrait = sonia ? [sonia stringByConvertingHTMLToPlainText] : @"[No Title]";
cCell.c2Label2.text = itemExtrait;
}
return cCell;
}
UITableViewCell的代码:
#import "ListNewsCell.h"
@implementation ListNewsCell
@synthesize c1Image1, c1Image2, c1Label1, c1Label2;
@synthesize c2Image1, c2Image2, c2Label1, c2Label2;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
c1Image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,154)];
c1Image1.backgroundColor = [UIColor clearColor];
[self addSubview:c1Image1];
[c1Image1 release];
c1Image2 = [[UIImageView alloc] initWithFrame:CGRectMake(0,98,320,56)];
c1Image2.backgroundColor = [UIColor clearColor];
[self addSubview:c1Image2];
[c1Image2 release];
c1Label1 = [[UILabel alloc] initWithFrame:CGRectMake(5, 100, 310, 20)];
c1Label1.font = [UIFont fontWithName:@"Arial-BoldMT" size:14];
c1Label1.textColor = [UIColor whiteColor];
c1Label1.backgroundColor = [UIColor clearColor];
[self addSubview:c1Label1];
c1Label2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 112, 310, 45)];
c1Label2.font = [UIFont fontWithName:@"Arial" size:12];
c1Label2.textColor = [UIColor whiteColor];
c1Label2.numberOfLines = 2;
c1Label2.backgroundColor = [UIColor clearColor];
[self addSubview:c1Label2];
c2Image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,70)];
c2Image1.backgroundColor = [UIColor clearColor];
[self addSubview:c2Image1];
[c2Image1 release];
c2Label1 = [[UILabel alloc] initWithFrame:CGRectMake(105, 8, 180, 20)];
c2Label1.font = [UIFont fontWithName:@"Arial-BoldMT" size:14];
c2Label1.textColor = [UIColor blackColor];
c2Label1.backgroundColor = [UIColor clearColor];
[self addSubview:c2Label1];
c2Label2 = [[UILabel alloc] initWithFrame:CGRectMake(105, 25, 180, 45)];
c2Label2.font = [UIFont fontWithName:@"Arial" size:12];
c2Label2.textColor = [UIColor blackColor];
c2Label2.numberOfLines = 2;
c2Label2.backgroundColor = [UIColor clearColor];
[self addSubview:c2Label2];
c2Image2 = [[UIImageView alloc] initWithFrame:CGRectMake(20,10,75,55)];
c2Image2.backgroundColor = [UIColor clearColor];
[self addSubview:c2Image2];
[c2Image2 release];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
- (void)dealloc
{
[super dealloc];
}
@end
谢谢大家 以下行导致大延迟:
cCell.c2Image2.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",caroline]]]];
每次加载其中一个单元格时,它都会发出URL请求。尝试预加载一组数据,而不是每个单元格请求数据
加载图像一次,使其成为类变量,然后重用其指针,如下所示:
cCell.c2Image2.image=预编辑图像代码>
在.h文件中声明:
UIImage * preloadedImage;
在-(void)loadView-方法中,输入:
preloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",caroline]]]];
还请注意,正如jrturton所写,您希望重用已出列的单元以获得额外的性能提升。以下行会导致较大的延迟:
cCell.c2Image2.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",caroline]]]];
每次加载其中一个单元格时,它都会发出URL请求。尝试预加载一组数据,而不是每个单元格请求数据
加载图像一次,使其成为类变量,然后重用其指针,如下所示:
cCell.c2Image2.image=预编辑图像代码>
在.h文件中声明:
UIImage * preloadedImage;
在-(void)loadView-方法中,输入:
preloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",caroline]]]];
还请注意,正如jrturton所写,您希望重用已出列的单元以获得额外的性能提升。您永远不会重用已出列的单元,因此每次都是从头开始重新创建所有内容。由于每行都有一个唯一的单元格(第2行及以上的单元格真的需要这样做吗?)
因此,您可以将dequeueReusableCellWithIdentifier
之后的所有内容包装到if(cCell==nil)
(显然,return
语句除外)中。您永远不会重用已退出队列的单元格,因此每次都是从头开始重新创建所有内容。由于每行都有一个唯一的单元格(第2行及以上的单元格真的需要这样做吗?)
因此,您可以将dequeueReusableCellWithIdentifier
之后的所有内容包装在if(cCell==nil)
中(显然,return
语句除外)。在cellforrowatinexpath方法中,您已经从表视图中将一些单元格排了队。但你从未使用过这种价值观。这导致了每次加载而不是重复使用的问题
尝试下面的代码,我在其中做了一些修改
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
[myTextField辞职FirstResponder]
ListNewsCell *cCell = (ListNewsCell *)[myTable dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cCellIdentifier%d",indexPath.row]];
如果(cCell==nil)
{
cCell=[[ListNewsCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:[NSString stringWithFormat:@“cCellIdentifier%d”,indexath.row]]
}
}
谢谢,
Arun.在cellForRowAtIndexPath方法中,您已将一些单元格从表视图中移出队列。但你从未使用过这种价值观。这导致了每次加载而不是重复使用的问题
尝试下面的代码,我在其中做了一些修改
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
[myTextField辞职FirstResponder]
ListNewsCell *cCell = (ListNewsCell *)[myTable dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cCellIdentifier%d",indexPath.row]];
如果(cCell==nil)
{
cCell=[[ListNewsCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:[NSString stringWithFormat:@“cCellIdentifier%d”,indexath.row]]
}
}
谢谢,
阿伦。我明白了,当我滚动我的tableview时,我会重新加载单元格中的所有图像。但是我怎样才能避免这种情况并在“缓存”中保存图像呢?更新了我对您问题的回答我看到了,当我滚动我的tableview时,我会重新加载单元格中的所有图像。但是我怎样才能避免这种情况并在“缓存”中保存一个图像呢?更新了我对你的问题的回答这也是正确的,但与每次加载单元格时执行同步HTTP请求相比,这种效果相当小。@Wolfert-很好-做我的回答时,他仍然会有缓慢的滚动初始我现在明白了,我添加如果(cCell==nil){…}
在dequeueReusableCellWithIdentifier
之后,但现在我有了一个白色屏幕。就像没有任何一个元素显示在单元格中……这也是事实,但与每次加载单元格时执行同步HTTP请求相比,这种效果相当小。@Wolfert-很好-做我的回答时,他仍然会有缓慢的滚动iniTially我现在明白了,我在dequeueReusableCellWithIdentifier
之后添加了if(cCell==nil){…}
,但现在我有了一个白色屏幕。就像没有任何元素显示在单元格中一样。。。