2011-10-18 14 views
0

3種類のセルを使用してアプリケーションでUITableview(IB付き)を作成します。私のテーブルビューをドラッグするときを除いてすべて正常に動作すべて非常に遅くなります。だから誰かが私のアプリのパフォーマンスを改善するために助けることができるなら、それはクールだ。テーブルビューのためのパフォーマンスの問題を伴うUITableView

マイコード:

- (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 

ありがとう!

答えて

2

次の行は、大きな遅延が発生します。

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

毎回これらの細胞のいずれかがロードされます、それはURLリクエストを行います。データをセルごとに要求するのではなく、あらかじめロードしてください。あなたの.hファイルで宣言し

cCell.c2Image2.image = preloadedImage;

ロード一度画像は、その後、同じようにそのポインタを再利用し、そのクラス変数作る

UIImage * preloadedImage; 

のとで- (void)loadView -method put:

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

また、jrturtonが書いたように、デキューされたセルを余分なパフォーマンス向上のために再利用したいと考えています。

+0

私はテーブルビューをスクロールすると、セル内のすべての画像を再読み込みします。しかし、どうすればこの問題を回避し、画像の「キャッシュ」を維持できますか? –

+0

あなたの質問への私の答えを更新しました – Wolfert

1

デキューされたセルを再利用することはないため、毎回すべてをゼロから再作成しています。各行に固有のセルがあるので(2行目以降のセルには本当に必要ですか?)、それらを一度作成して作成するだけで済みます。

したがって、dequeueReusableCellWithIdentifier以降のすべてをif (cCell == nil)(おそらくreturnのステートメントは除いて)内に折り返すことができます。

+1

それも当てはまりますが、セルをロードするたびにシンクロムHTTPリクエストを行うのに比べて、これの効果はかなり小さいです。 – Wolfert

+0

@Wolfert - 良い点 - 彼はまだ遅いスクロールを最初に持っていただろう – jrturton

+0

私は今理解し、 'de(DeCue == nil){...}'を 'dequeueReusableCellWithIdentifier'の後に追加するが、今は白い画面がある。それは、細胞内に表示される要素がないようなものです... –

0

cellForRowAtIndexPathメソッドでは、テーブルビューからいくつかのセルをデキューしました。しかし、あなたはその価値を使ったことはありません。そのため、再使用するのではなく、毎回ロードするという問題が発生しました。

..私は少し変更したした次のコードを試してみてください - (UITableViewCellの*)のtableView:(のUITableView *)のtableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

[myTextField resignFirstResponder];

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

(cCell == NIL)

{

cCell = [[ListNewsCell ALLOC] initWithStyle場合:UITableViewCellStyleDefault reuseIdentifier:[NSStringのstringWithFormat: "cCellIdentifier%dの" @、indexPath。行]];

}

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

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

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

}

おかげで、 アルン。

関連する問題