2010-12-14 11 views
0

Ich habe eine scrollbare Tabelle. In jeder Zelle zeichne ich 3 UILabels. Die ersten paar Zellen zeichnen sich gut aus. Aber während ich den Tisch herunterscrolle, scheint das UILabels über die vorherigen UILabels zu ziehen. Wie bei den Labels in der Zelle gibt es bereits ein altes Label, das gelöscht wurde. Ich könnte das wahrscheinlich beheben, indem ich eine Hintergrundfarbe über die gesamte Zelle zeichne, aber das erklärt immer noch nicht dieses seltsame Problem und warum es passiert.Scrollbare Tabelle mit einem Redraw-Problem. Scheint nicht zu löschen

Jeder weiß, warum das passiert und was eine Lösung sein könnte?

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell";  
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier] autorelease]; 
} 

Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row]; 

UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)]; 
teamName1Label.textAlignment = UITextAlignmentCenter; 
teamName1Label.textColor = [UIColor redColor]; 
teamName1Label.backgroundColor = [UIColor clearColor]; 
teamName1Label.text = aMatch.teamName1; 
[cell addSubview:teamName1Label]; 

UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)]; 
teamVersusLabel.textAlignment = UITextAlignmentCenter; 
teamVersusLabel.textColor = [UIColor redColor]; 
teamVersusLabel.backgroundColor = [UIColor clearColor]; 
teamVersusLabel.text = @"V"; 
[cell addSubview:teamVersusLabel]; 


UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)]; 
teamName2Label.textAlignment = UITextAlignmentCenter; 
teamName2Label.textColor = [UIColor redColor]; 
teamName2Label.backgroundColor = [UIColor clearColor]; 
teamName2Label.text = aMatch.teamName2; 
[cell addSubview:teamName2Label]; 

return cell; 
} 

Vielen Dank -Code

Antwort

4

Ich denke, die beste Lösung für dieses Problem, diese Etiketten in den zu definieren ist - (UITableViewCell *) reuseTableViewCellWithIdentifier: (NSString *) Kennung withIndexPath: (NSIndexPath *) indexPath Methode

-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath{ 
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease]; 

UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)]; 
teamName1Label.textAlignment = UITextAlignmentCenter; 
teamName1Label.textColor = [UIColor redColor]; 
teamName1Label.backgroundColor = [UIColor clearColor]; 
teamName1Label.text = aMatch.teamName1; 
teamName1Label.tag = 1; 
[cell.contentView addSubview:teamName1Label]; 
[teamName1Label release]; 

UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)]; 
teamVersusLabel.textAlignment = UITextAlignmentCenter; 
teamVersusLabel.textColor = [UIColor redColor]; 
teamVersusLabel.backgroundColor = [UIColor clearColor]; 
teamVersusLabel.text = @"V"; 
teamVersusLabel.tag = 2; 
[cell.contentView addSubview:teamVersusLabel]; 
[teamVersusLabel release]; 


UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)]; 
teamName2Label.textAlignment = UITextAlignmentCenter; 
teamName2Label.textColor = [UIColor redColor]; 
teamName2Label.backgroundColor = [UIColor clearColor]; 
teamName2Label.text = aMatch.teamName2; 
teamName2Label.tag = 3; 
[cell.contentView addSubview:teamName2Label]; 
[teamName2Label release]; 

return cell; 
} 

Jetzt die cellForRowAtIndexPath Methode be-- versuchen

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

    UITableViewCell *cell=nil; 

    static NSString *Identifier = @"Cell"; 
    cell = [theTableView dequeueReusableCellWithIdentifier:Identifier]; 
    if(cell == nil){ 
    cell = [self reuseTableViewCellWithIdentifier:Identifier withIndexPath:indexPath]; 
    } 

    Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row]; 

    UILabel *label = (UILabel *)[cell.contentView viewWithTag:1]; 
    label.text = aMatch.teamName1; 

    label = (UILabel *)[cell.contentView viewWithTag:2]; 
    label.text = @"V"; 

    label = (UILabel *)[cell.contentView viewWithTag:3]; 
    label.text = aMatch.teamName2; 

    return cell; 
    } 

einfach dieses code..Hope es hilft :)

0

Dies ist eine Zelle wiederverwendet Problem. Der Code fügt der Zelle jedes Mal, wenn cellForRowAtIndexPath aufgerufen wird, die Beschriftungen hinzu (auch wenn Zelle nicht null ist).

Wenn Sie scrollen, wird eine vorherige Zelle geladen, die bereits die Beschriftungen enthält, und dann fügt der Code neue Beschriftungen über die alten hinzu.

Erstellen und fügen Sie die Beschriftungen nur zur Zelle im Abschnitt if (cell == nil) hinzu. Wenn Sie die Etiketten erstellen, legen Sie Tags auf den Etiketten fest.

Dann außerhalb der if, die Etiketten mit ihrem Tag mit viewWithTag abrufen und ihren Text festlegen.

Verwandte Themen