2011-07-27 143 views
3

當我滾動我的UITableView時,由於某些原因,單元格似乎彼此相鄰。如果我加載了我的應用程序,細胞會出現這樣的事情:UITableView在滾動上重複單元格

enter image description here

並且在屏幕上的電源,然後再滾動該小區多次,它會開始出現這樣的:

enter image description here

正如你所看到的東西我似乎無法制定出是哪裏錯了。有任何想法嗎?

編輯:的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中的任何視圖。這些分配是減慢應該順利滾動的tableView中的滾動。你基本上需要做的是設置你在單元中需要的所有視圖,當你離開時沒有得到一個視圖。當你接收到一個出隊單元時,你只能從某個模型對象或類似的東西上設置它的狀態。

相關問題