2011-11-25 68 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