2011-11-25 45 views
0

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

任何人都遇到过这样的问题,可以给我一个提示吗?

编辑: 自定义代码的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]; 
} 

的cellForRowAtIndexPath在UITableView的

- (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; 
} 
+1

向我们展示一些代码。 – rckoenes

回答

3

当你正在重用的细胞,当细胞问- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath它不是从重新加载你的笔尖。 所以,如果你设置的笔尖例如标签颜色为黑色,并在您的代码

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处添加另一个子层BEHIND subLayer1。您可能需要先删除旧的自定义subLayer。

+0

(编辑我的第一篇文章)我不明白,为什么它使用标签的文字,但不是与颜色?我对iPhone的开发很不熟悉。 – Pathos

+0

好的,我明白了,我编辑了我的答案。 – Zoleas

+0

非常好,谢谢你,工作。太糟糕了,这个问题降低了我的声誉^^ – Pathos