当我滚动我的UITableView时,出于某种原因,单元格似乎相互绘制。如果我加载我的应用程序,单元格将显示如下:
在滚动此单元格并多次在屏幕上滚动时,它将开始显示如下:
正如你所看到的,我似乎无法解决的问题是错误的。有什么想法吗?
编辑: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:Cell:
//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 :(得分:6)
每次显示时,你只是在你的手机中添加越来越多的UILabel等!
您需要跟踪要添加到单元格中的所有内容,以确保只添加一次!有几种方法可以做到这一点,但我建议你自己继承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";
所以,第一次创建单元格时,就会创建标签。你要求创建单元格的所有其他时间,你检查标签是否已经存在,如果是,只需更新它的文本。
答案 1 :(得分:2)
我想你一直在为单元格的内容视图添加一些视图。只有在看到代码后才能找到解决方案。
修改强>
实际上我已经为你发布的代码提供了解决方案。但我也建议按照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:Cell:
//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:Cell:
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;
答案 2 :(得分:2)
@ Gomathi的直觉是正确的。你在这里找到一个回收的细胞:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
此单元格已包含所有视图。然后,您将再次添加视图。当您从dequeueReusableCellWithIdentifier:
返回一个单元格时,您应该重新配置它(更改文本和图像字段的值),而不是从头开始重建它。这是可重复使用细胞的重点。请务必阅读Table View Programming Guide了解完整详情。
答案 3 :(得分:1)
单元重用的重点是仅在未从dequeueReusableCellWithIdentifier
返回单元格时才在UITableViewCell中分配任何视图。 allocs减慢了应该滚动顺畅的tableView中的滚动。你基本上需要做的是,当你出局时没有得到你需要的所有视图。当你收到一个出列的单元格时,你应该只是从某个模型对象或类似物体上设置状态。