Ios iPhone-重用TableViewCells

Ios iPhone-重用TableViewCells,ios,objective-c,iphone,uitableview,cocoa-touch,Ios,Objective C,Iphone,Uitableview,Cocoa Touch,在我的iPhone应用程序中,我正在使用自定义单元格的UITableView。这些单元格包含不同的标签,主要表示文本。但我使用其中一个标签作为某种状态指示器,给它不同的颜色。 由于滚动变得非常慢,我想重新使用单元格。这样做给了我很好的性能提升,但“状态标签”显示错误。不同单元格的颜色不再正确(所有其他标签都正确) 有谁经历过这样的问题,能给我一个提示吗 编辑: 自定义tablecell的代码 - (void)setIncident:(Incident *)_incident{ [self set

在我的iPhone应用程序中,我正在使用自定义单元格的UITableView。这些单元格包含不同的标签,主要表示文本。但我使用其中一个标签作为某种状态指示器,给它不同的颜色。 由于滚动变得非常慢,我想重新使用单元格。这样做给了我很好的性能提升,但“状态标签”显示错误。不同单元格的颜色不再正确(所有其他标签都正确)

有谁经历过这样的问题,能给我一个提示吗

编辑: 自定义tablecell的代码

- (void)setIncident:(Incident *)_incident{
[self setSelectionStyle:UITableViewCellEditingStyleNone];
incident = _incident;

streamNameLbl.text = incident.streamName;
jobNameLbl.text = incident.jobName;
workInProgressByLbl.text = incident.workInProgressBy;
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc] init];
[tempFormatter setDateFormat:@"hh:mm"];
errorTimeLbl.text = [NSString stringWithFormat:@"%@", [tempFormatter stringFromDate:incident.errorTime]];

[tempFormatter setDateFormat:@"dd. MMM"];
plandateLbl.text = [NSString stringWithFormat:@"%@", [tempFormatter stringFromDate:incident.planDate]];

returnCodeLbl.text = [@"RC: " stringByAppendingString: incident.returnCode];
runNumberLbl.text = [@"Run: " stringByAppendingString: incident.runNumber];
severityLbl.text = incident.severity;
restartStatusLblValue.text = incident.restartStatus;

    NSString * color = incident.severityColor;
    NSString * colorR = [color substringWithRange:NSMakeRange(1, 2)];
    NSString * colorG = [color substringWithRange:NSMakeRange(3, 2)];
    NSString * colorB = [color substringWithRange:NSMakeRange(5, 2)];

    unsigned intColorR = 0;
    unsigned intColorG = 0;
    unsigned intColorB = 0;

    NSScanner *scanner = [NSScanner scannerWithString:colorR];
    [scanner scanHexInt:&intColorR];
    scanner = [NSScanner scannerWithString:colorG];
    [scanner scanHexInt:&intColorG];
    scanner = [NSScanner scannerWithString:colorB];
    [scanner scanHexInt:&intColorB];

    incidentStatusLbl.backgroundColor = [UIColor clearColor];

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = incidentStatusLbl.bounds;
    gradient.startPoint = CGPointMake(0, 0.5);
    gradient.endPoint = CGPointMake(1, 0.5);
    UIColor * startColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    UIColor * endColor = [UIColor colorWithRed:intColorR/255.0 green:intColorG/255.0 blue:intColorB/255.0 alpha:1];
    gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];

    [incidentStatusLbl.layer insertSublayer:gradient atIndex:0];
}
UITable视图中的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

incidentCell = (IncidentCell *)[tableView dequeueReusableCellWithIdentifier:@"IncidentCell"];
if (incidentCell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"IncidentCell" owner:self options:nil];
    incidentCell = [nib objectAtIndex:0];
     NSLog(@"Loading cell from xib file");
    }
else{
    NSLog(@"Reusing cell");    
     }

NSMutableArray *sectionDetails = ((NSMutableArray *)[incidentDic objectForKey:[self.sortedSections objectAtIndex:[indexPath section]]]);

Incident *incident = [sectionDetails objectAtIndex:[indexPath row]];

[incidentCell setIncident:incident];

return incidentCell;
}

在重用单元格时,当在
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nsindepath*)indepath
中询问单元格时,不会从nib重新加载该单元格。 例如,如果在笔尖中将标签颜色设置为黑色,并且在代码中设置为黑色

if (some_condition) {
   cell.myLabel.textcolor= myStatusColor;
}
当一个标签有一次myStatusColor颜色时,当您重新使用它时,它将保留它

所以你必须这么做

if (some_condition) {
   cell.myLabel.textcolor= myStatusColor;
} else {
   cell.myLabel.textcolor= [UIColor black];
}
事实上,您的问题来自
[incidentStatusLbl.layer insertSublayer:gradient atIndex:0]


如果您使用它一次,您将拥有一个子层(我们称之为subLayer1)。当您重新使用单元格时,您将在索引0处的子层1后面添加另一个子层。您可能希望在以前删除旧的自定义子层。

在重用单元格时,当在
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath中询问单元格时,不会从nib重新加载该单元格。
例如,如果在笔尖中将标签颜色设置为黑色,并且在代码中设置为黑色

if (some_condition) {
   cell.myLabel.textcolor= myStatusColor;
}
当一个标签有一次myStatusColor颜色时,当您重新使用它时,它将保留它

所以你必须这么做

if (some_condition) {
   cell.myLabel.textcolor= myStatusColor;
} else {
   cell.myLabel.textcolor= [UIColor black];
}
事实上,您的问题来自
[incidentStatusLbl.layer insertSublayer:gradient atIndex:0]


如果您使用它一次,您将拥有一个子层(我们称之为subLayer1)。当您重新使用单元格时,您将在索引0处的子层1后面添加另一个子层。您可能想删除以前的自定义子层。

(编辑了我的第一篇文章)我不明白的是,为什么它只处理标签的文本,而不处理颜色?我对iphone的开发还没有经验。非常好,谢谢你,成功了。太糟糕了,这个问题降低了我的声誉^^(编辑了我的第一篇文章)我不明白,为什么它只处理标签的文本,而不处理颜色?我对iphone的开发还没有经验。非常好,谢谢你,成功了。可惜这个问题降低了我的声誉^^