2011-07-27 10 views
3

私は自分のUITableViewをスクロールするとき、何らかの理由でセルが互いに描画されているようです。私は私のアプリをロードした場合、セルは次のように表示されます:スクロールでUITableViewのセルを繰り返す

enter image description here

を、画面上のオフ、バック回数をこのセルをスクロールすると、それは次のように表示されるように始めましょう:

enter image description here

あなたが見ることができるように、私が動作するように見えることはできません何かが間違って起こっています。何か案は?

EDIT:cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSString *vaultsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Vaults"]; 

    NSString *dictionaryPath = [NSString stringWithFormat:@"%@/%@", 
           vaultsPath, 
           [self.vaults objectAtIndex:indexPath.row]]; 
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath]; 
    cell = [AHCellCreation createCellWithDictionary:dictionary Cell:cell]; 

    return cell; 

AHCellCreation + createCellWithDictionary:細胞:

//General cell design, same every time 
CAGradientLayer *gradient = [CAGradientLayer layer]; 
gradient.frame = CGRectMake(0, 0, 320, 82); 
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithHue:0 saturation:0 brightness:0.91 alpha:1] CGColor], (id)[[UIColor colorWithHue:0 saturation:0 brightness:0.85 alpha:1] CGColor], nil]; 
[cell.contentView.layer addSublayer:gradient]; 

UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)]; 
topLine.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.97 alpha:1.0]; 
[cell addSubview:topLine]; 

UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 81, 320, 1)]; 
bottomLine.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.64 alpha:1.0]; 
[cell addSubview:bottomLine]; 

//Preview Image 
NSString *previewImageFilePath = [dictionary objectForKey:@"PreviewImage"]; 

UIImageView *previewImageView = [[UIImageView alloc] initWithFrame:CGRectMake(9, 9, 64, 64)]; 
previewImageView.image = [UIImage imageWithContentsOfFile:previewImageFilePath]; 
[cell addSubview:previewImageView]; 

//Creation date 
UILabel *createdOnLabel = [[UILabel alloc] init]; 
createdOnLabel.frame = CGRectMake(85, -5, 303, 41); 
createdOnLabel.text = @"Created on"; 
createdOnLabel.backgroundColor = [UIColor clearColor]; 
createdOnLabel.textAlignment = UITextAlignmentLeft; 
createdOnLabel.font = [UIFont systemFontOfSize:12]; 
createdOnLabel.textColor = [UIColor colorWithHue:0.59 saturation:0.29 brightness:0.47 alpha:1.0]; 
[cell addSubview:createdOnLabel]; 

NSDate *creationDate = [dictionary objectForKey:@"CreationDate"]; 
UILabel *creationDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 0, 303, 82)]; 
creationDateLabel.text = [AHCellCreation createReadableDateFromDate:creationDate]; 
creationDateLabel.backgroundColor = [UIColor clearColor]; 
creationDateLabel.textAlignment = UITextAlignmentLeft; 
creationDateLabel.font = [UIFont boldSystemFontOfSize:28]; 
creationDateLabel.textColor = [UIColor colorWithHue:0.59 saturation:0.29 brightness:0.47 alpha:1.0]; 
[cell addSubview:creationDateLabel]; 

//Opening date 
NSDate *notificationDate = [dictionary objectForKey:@"NotificationDate"]; 

NSDate *earliest = [notificationDate earlierDate:[NSDate date]]; 
BOOL notificationPassed; 
if (earliest == [NSDate date]) { 
    notificationPassed = YES; 
} 
else { 
    notificationPassed = NO; 
} 

UILabel *notificationDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 47, 303, 41)]; 
if (notificationPassed == NO) { 
    notificationDateLabel.text = @"To be opened"; 
} 
else { 
    notificationDateLabel.text = @"Opened on"; 
} 
notificationDateLabel.backgroundColor = [UIColor clearColor]; 
notificationDateLabel.textAlignment = UITextAlignmentLeft; 
notificationDateLabel.font = [UIFont systemFontOfSize:12]; 
notificationDateLabel.textColor = [UIColor colorWithHue:0.59 saturation:0.29 brightness:0.47 alpha:1.0]; 
[cell addSubview:notificationDateLabel]; 

UILabel *notificationDateLabel2 = [[UILabel alloc] init]; 
notificationDateLabel2.frame = CGRectMake(164, 47, 303, 41); 
notificationDateLabel2.text = [AHCellCreation createReadableDateFromDate:notificationDate]; 
notificationDateLabel2.backgroundColor = [UIColor clearColor]; 
notificationDateLabel2.textAlignment = UITextAlignmentLeft; 
notificationDateLabel2.font = [UIFont boldSystemFontOfSize:12]; 
notificationDateLabel2.textColor = [UIColor colorWithHue:0.59 saturation:0.29 brightness:0.47 alpha:1.0]; 
[cell addSubview:notificationDateLabel2]; 


return cell; 
+0

cellForRowAtIndexPathメソッドでセルを再利用する際に問題があります。そのコードを投稿すれば、正確に何が間違っているのかを教えることができます – Vladimir

+0

cellforrowatindexpath関数を貼り付け –

+0

そのコードで更新しました – Andrew

答えて

6

表示されるたびに、セルにUILabelsなどを追加するだけです!

セルに追加しているすべてのものを追跡して、一度だけ追加していることを確認する必要があります。そこにこれを行うためのいくつかの方法がありますが、私はサブクラスは、例えば自分で

をUITableViewCellのお勧め、ここcreatedOnLabelが正しく動作して取得するためのコードです:

@interface AHTableViewCell : UITAbleViewCell { 
    UILabel *createdOnLabel; 
} 

@end 

@implementation AHTableViewCell 

@end 

次に、あなたのcellForRowAtIndexPathコードが

AHTableViewCell *cell = (AHTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[AHTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
なり

あなたの作成コードは次のようになります:

//Creation date 
UILabel *createdOnLabel = [cell createdOnLabel]; 
if (nil == createdOnLabel) { 
    createdOnLabel = [[UILabel alloc] init]; 
    createdOnLabel.frame = CGRectMake(85, -5, 303, 41); 
    createdOnLabel.backgroundColor = [UIColor clearColor]; 
    createdOnLabel.textAlignment = UITextAlignmentLeft; 
    createdOnLabel.font = [UIFont systemFontOfSize:12]; 
    createdOnLabel.textColor = [UIColor colorWithHue:0.59 saturation:0.29 brightness:0.47 alpha:1.0]; 
    [cell addSubview:createdOnLabel]; 
    [cell setCreatedOnLabel:createdOnLabel]; 
} 
createdOnLabel.text = @"Created on"; 

したがって、初めてセルを作成するときは、ラベルを作成します。あなたがセルを作成するように依頼する他のすべての時間は、ラベルがすでに存在するかどうかを確認し、存在する場合はテキストを更新するだけです。

2

私はuは、セルのコンテンツビューにいくつかのビューを追加することで維持されていると思います。ソリューションは、コードを見た後でしか見つかりません。

編集:

は、実際に私はuが掲載されているコードのためのソリューションを与えています。しかし、私はまた、deanWombourneが提案したように、UITableViewCellのサブクラス化をお勧めします。

はい、私の推測は正しいです。

まず、cellForRowAtIndexPathを次のように作成します。

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    NSString *vaultsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Vaults"]; 
    NSString *dictionaryPath = [NSString stringWithFormat:@"%@/%@", 
          vaultsPath, 
          [self.vaults objectAtIndex:indexPath.row]]; 
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath]; 

    cell = [AHCellCreation createCellWithDictionary:dictionary Cell:cell]; 

} 
else 
{ 
    NSString *vaultsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Vaults"]; 

    NSString *dictionaryPath = [NSString stringWithFormat:@"%@/%@", 
           vaultsPath, 
           [self.vaults objectAtIndex:indexPath.row]]; 
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:dictionaryPath]; 
    cell = [AHCellCreation updateCellWithDictionary:dictionary Cell:cell]; 
} 


return cell; 

AHCellCreation + createCellWithDictionary:細胞:

//General cell design, same every time 
CAGradientLayer *gradient = [CAGradientLayer layer]; 
gradient.frame = CGRectMake(0, 0, 320, 82); 
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithHue:0 saturation:0 brightness:0.91 alpha:1] CGColor], (id)[[UIColor colorWithHue:0 saturation:0 brightness:0.85 alpha:1] CGColor], nil]; 
[cell.contentView.layer addSublayer:gradient]; 

UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)]; 
topLine.tag = 100; 
topLine.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.97 alpha:1.0]; 
[cell addSubview:topLine]; 

UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 81, 320, 1)]; 
bottomLine.tag = 101; 
bottomLine.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.64 alpha:1.0]; 
[cell addSubview:bottomLine]; 

//Preview Image 
NSString *previewImageFilePath = [dictionary objectForKey:@"PreviewImage"]; 

UIImageView *previewImageView = [[UIImageView alloc] initWithFrame:CGRectMake(9, 9, 64, 64)]; 
previewImageView.tag = 102; 
previewImageView.image = [UIImage imageWithContentsOfFile:previewImageFilePath]; 
[cell addSubview:previewImageView]; 

//Creation date 
UILabel *createdOnLabel = [[UILabel alloc] init]; 
createdOnLabel.tag = 103; 
createdOnLabel.frame = CGRectMake(85, -5, 303, 41); 
createdOnLabel.text = @"Created on"; 
createdOnLabel.backgroundColor = [UIColor clearColor]; 
createdOnLabel.textAlignment = UITextAlignmentLeft; 
createdOnLabel.font = [UIFont systemFontOfSize:12]; 
createdOnLabel.textColor = [UIColor colorWithHue:0.59 saturation:0.29 brightness:0.47 alpha:1.0]; 
[cell addSubview:createdOnLabel]; 

NSDate *creationDate = [dictionary objectForKey:@"CreationDate"]; 
UILabel *creationDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 0, 303, 82)]; 
creationDateLabel.tag = 104; 
creationDateLabel.text = [AHCellCreation createReadableDateFromDate:creationDate]; 
creationDateLabel.backgroundColor = [UIColor clearColor]; 
creationDateLabel.textAlignment = UITextAlignmentLeft; 
creationDateLabel.font = [UIFont boldSystemFontOfSize:28]; 
creationDateLabel.textColor = [UIColor colorWithHue:0.59 saturation:0.29 brightness:0.47 alpha:1.0]; 
[cell addSubview:creationDateLabel]; 

//Opening date 
NSDate *notificationDate = [dictionary objectForKey:@"NotificationDate"]; 

NSDate *earliest = [notificationDate earlierDate:[NSDate date]]; 
BOOL notificationPassed; 
if (earliest == [NSDate date]) { 
    notificationPassed = YES; 
} 
else { 
    notificationPassed = NO; 
} 

UILabel *notificationDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, 47, 303, 41)]; 
notificationDateLabel.tag = 105; 
if (notificationPassed == NO) { 
    notificationDateLabel.text = @"To be opened"; 
} 
else { 
    notificationDateLabel.text = @"Opened on"; 
} 
notificationDateLabel.backgroundColor = [UIColor clearColor]; 
notificationDateLabel.textAlignment = UITextAlignmentLeft; 
notificationDateLabel.font = [UIFont systemFontOfSize:12]; 
notificationDateLabel.textColor = [UIColor colorWithHue:0.59 saturation:0.29 brightness:0.47 alpha:1.0]; 
[cell addSubview:notificationDateLabel]; 

UILabel *notificationDateLabel2 = [[UILabel alloc] init]; 
notificationDateLabel.tag = 106; 
notificationDateLabel2.frame = CGRectMake(164, 47, 303, 41); 
notificationDateLabel2.text = [AHCellCreation createReadableDateFromDate:notificationDate]; 
notificationDateLabel2.backgroundColor = [UIColor clearColor]; 
notificationDateLabel2.textAlignment = UITextAlignmentLeft; 
notificationDateLabel2.font = [UIFont boldSystemFontOfSize:12]; 
notificationDateLabel2.textColor = [UIColor colorWithHue:0.59 saturation:0.29 brightness:0.47 alpha:1.0]; 
[cell addSubview:notificationDateLabel2]; 


return cell; 

AHCellCreation + updateCellWithDictionary:細胞:

UIView *topLine = (UIView*)[cell viewWithTag:100]; 
topLine.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.97 alpha:1.0]; 

UIView *bottomLine = (UIView*)[cell viewWithTag:101]; 
bottomLine.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.64 alpha:1.0]; 

//Preview Image 
NSString *previewImageFilePath = [dictionary objectForKey:@"PreviewImage"]; 

UIImageView *previewImageView = (UIImageView*)[cell viewWithTag:102]; 
previewImageView.image = [UIImage imageWithContentsOfFile:previewImageFilePath]; 

//Creation date 
UILabel *createdOnLabel = (UILabel*)[cell viewWithTag:103]; 
createdOnLabel.text = @"Created on"; 

NSDate *creationDate = [dictionary objectForKey:@"CreationDate"]; 
UILabel *creationDateLabel = (UILabel*)[cell viewWithTag:104]; 
creationDateLabel.text = [AHCellCreation createReadableDateFromDate:creationDate]; 

//Opening date 
NSDate *notificationDate = [dictionary objectForKey:@"NotificationDate"]; 

NSDate *earliest = [notificationDate earlierDate:[NSDate date]]; 
BOOL notificationPassed; 
if (earliest == [NSDate date]) { 
    notificationPassed = YES; 
} 
else { 
    notificationPassed = NO; 
} 

UILabel *notificationDateLabel = (UILabel*)[cell viewWithTag:105]; 
if (notificationPassed == NO) { 
    notificationDateLabel.text = @"To be opened"; 
} 
else { 
    notificationDateLabel.text = @"Opened on"; 
} 

UILabel *notificationDateLabel2 = (UILabel*)[cell viewWithTag:106]; 
notificationDateLabel2.text = [AHCellCreation createReadableDateFromDate:notificationDate];  

return cell; 
+0

コードを追加しました – Andrew

+1

私はセルの作成と更新メソッドで投稿を編集しました。これを試して。楽しむ。 – Ilanchezhian

2

Gomathiの直感@は正しかったです。あなたはここにリサイクルセルを取得している:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

このセルは、すでにその中のすべてのビューを持っています。次に、ビューを再度追加します。 dequeueReusableCellWithIdentifier:からセルを取り戻すときは、テキストフィールドとイメージフィールドの値を変更して、最初から再構築する必要はありません。これが再利用可能な細胞の全体像です。詳細についてはTable View Programming Guideを必ずお読みください。

1

セル再利用のポイントは、dequeueReusableCellWithIdentifierからセルが返されない場合にのみ、UITableViewCellの任意のビューを割り当てることです。 allocsはスムーズにスクロールしなければならないtableViewのスクロールを遅くするものです。あなたが基本的にやる必要があるのは、デキュー時にセルを取得しないときにセルに必要なすべてのビューをセットアップすることです。デキューされたセルを受け取ったときは、モデルオブジェクトなどから状態を設定するだけで済みます。

関連する問題