UITableView存在性能问题

时间:2011-10-18 12:59:02

标签: iphone ios performance uitableview

我在我的应用程序中使用3种类型的单元格创建了一个UITableview(使用IB)。所有工作都很好,除非我拖动我的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

感谢所有人!

3 个答案:

答案 0 :(得分:2)

以下行会造成很大的延迟:

cCell.c2Image2.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",caroline]]]];

每次加载其中一个单元格时,它都会生成一个URL请求。尝试预加载一堆数据,而不是每个单元格请求它。

加载图像一次,使其成为一个类变量,然后重复使用它的指针,如:

cCell.c2Image2.image = preloadedImage;

在您的.h文件中声明:

UIImage * preloadedImage;

- (void)loadView -method put:

preloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",caroline]]]];

另外请注意,正如jrturton写的那样,您希望重用已出列的单元格来提高性能。

答案 1 :(得分:1)

您永远不会重复使用已出列的单元格,因此您每次都会从头开始重新创建所有内容。由于每行都有一个唯一的单元格(这对第2行及以上的单元格来说真的有必要吗?),您只需要创建并填充一次。

所以,你可以在dequeueReusableCellWithIdentifier之后将if (cCell == nil)之后的所有内容包裹起来(显然除了return语句)。

答案 2 :(得分:0)

在cellForRowAtIndexPath方法中,您已从表视图中将某些单元格从队列中取出。但是你从未使用过这个价值。这导致了每次加载而不是重新使用的问题。

尝试以下代码,我修改了一些..

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

{

[myTextField resignFirstResponder];

ListNewsCell *cCell = (ListNewsCell *)[myTable dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cCellIdentifier%d",indexPath.row]];

if(cCell == nil)

{

cCell = [[ListNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@“cCellIdentifier%d”,indexPath.row]];

}

if (indexPath.row == 0) {
   // Some code here
    }

if (indexPath.row == 1) {        
    //Some code here
}

if (indexPath.row > 1) {
  // some code here
}
return cCell;

}

谢谢, 阿伦。