2011-07-27 146 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中的滚动。你基本上需要做的是设置你在单元中需要的所有视图,当你离开时没有得到一个视图。当你接收到一个出队单元时,你只能从某个模型对象或类似的东西上设置它的状态。

相关问题