2011-01-25 8 views
9

ho questo problema strano con la mia tavolaUITableViewCell - sovrapposizione con le cellule precedenti nel

  1. Ho circa 20 cellule per visualizzare
  2. Ogni cella è di circa 84px in altezza
  3. Quando clicco non la cella , ho impostato un colore di sfondo
  4. Le prime 4 celle sono ok, ma quando si scorre verso il basso e si fa clic sulla quinta cella, il contenuto di ogni cella inizia a sovrapporsi a qualche altro contenuto, di solito contenuto dalle prime 4 celle.

Credo che sia un problema di riutilizzo delle celle o di disegno. Non sono sicuro di come risolverlo, ho controllato il mio codice, ma non sto cambiando il contenuto della cella al tatto.

Ecco il mio codice e aggiungerà alcune foto troppo Cell with overlapped content

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 104; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [stores count]; 
} 

-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    CGRect Label1Frame = CGRectMake(5, 5, 250, 30); 
    CGRect Label2Frame = CGRectMake(6, 42, 220, 20);  
    CGRect Label3Frame = CGRectMake(6, 62, 220, 20);   
    CGRect Label4Frame = CGRectMake(240,56, 70, 12);    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    }else{ 
     // a cell is being recycled, remove the old edit field (if it contains one of our tagged edit fields) 
     UIView *viewToCheck = nil; 
     viewToCheck = [cell.contentView viewWithTag:1]; 
     if (!viewToCheck) { 
      [viewToCheck removeFromSuperview]; 
      DebugLog(@"View removed"); 
     }  
    } 
    //cell.selectionStyle=UITableViewCellSelectionStyleNone; 
    [cell setSelectedBackgroundView:bgView];  
    NSInteger row=indexPath.row; 
    UILabel *lblTemp; 
    [[cell contentView] clearsContextBeforeDrawing]; 

    //Line 1 

    lblTemp=[[UILabel alloc] initWithFrame: Label1Frame]; 
    lblTemp.tag=1; 
    lblTemp.text=[[stores objectAtIndex:row] objectAtIndex:0] ; 
    lblTemp.numberOfLines=2; 
    lblTemp.font = [UIFont boldSystemFontOfSize:13]; 
    lblTemp.adjustsFontSizeToFitWidth=YES; 
    lblTemp.minimumFontSize=12; 
    lblTemp.textColor = [UIColor grayColor]; 
    [cell.contentView addSubview:lblTemp]; 
    [lblTemp release]; 
    //Line 2 
    lblTemp = [[UILabel alloc] initWithFrame:Label2Frame]; 
    lblTemp.tag = 2; 
    lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:1]; 
    lblTemp.font = [UIFont systemFontOfSize:12]; 
    lblTemp.textColor = [UIColor grayColor ]; 
    lblTemp.textAlignment=UITextAlignmentLeft; 
    lblTemp.adjustsFontSizeToFitWidth=YES; 
    lblTemp.minimumFontSize=12; 

    [cell.contentView addSubview:lblTemp]; 
    [lblTemp release]; 

    //Line 3  
    lblTemp = [[UILabel alloc] initWithFrame:Label3Frame]; 
    lblTemp.tag = 3; 
    lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:2]; 
    lblTemp.font = [UIFont systemFontOfSize:12]; 
    lblTemp.textColor = [UIColor grayColor ]; 
    [cell.contentView addSubview:lblTemp];  
    [lblTemp release]; 
    //Phone button 
    UIButton *phoneButton=[[UIButton alloc] initWithFrame:CGRectMake(240,16,30,30)]; 
    [phoneButton setBackgroundImage:[UIImage imageNamed:@"phone.png"] forState:UIControlStateNormal]; 
    [phoneButton setTag:row]; 
    [phoneButton addTarget:self action:@selector(dialNumber:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:phoneButton]; 
    //ANnotation button 
    UIButton *annotation=[[UIButton alloc] initWithFrame:CGRectMake(274,16,30,30)]; 
    [annotation setTag:row]; 
    [annotation setBackgroundImage:[UIImage imageNamed:@"tab.png"] forState:UIControlStateNormal]; 
    [annotation addTarget:self action:@selector(openMap:) forControlEvents:UIControlEventTouchUpInside];  
    [cell.contentView addSubview:annotation]; 
    [annotation release]; 
    //Distance label 
    //Line 3  
    lblTemp = [[UILabel alloc] initWithFrame:Label4Frame]; 
    lblTemp.tag = 4; 
    lblTemp.text=[[stores objectAtIndex:row]objectAtIndex:5]; 
    lblTemp.textAlignment=UITextAlignmentCenter; 
    lblTemp.font = [UIFont systemFontOfSize:13]; 
    lblTemp.textColor = [UIColor grayColor ]; 
    [lblTemp setAdjustsFontSizeToFitWidth:YES]; 
    [cell.contentView addSubview:lblTemp];  
    [phoneButton release]; 
    [lblTemp release]; 
    [cell setNeedsLayout]; 
    [cell setNeedsDisplay]; 
    return cell; 
} 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath ]; 
    for(UILabel *lbl in cell.contentView.subviews){ 
     if([lbl isKindOfClass:[UILabel class]]){ 
      lbl.textColor=[UIColor whiteColor]; 

     } 
    } 
    //UITableViewCell *cell1;  
    //NSString *row=[NSString stringWithFormat:@"%d",indexPath.row]; 
    svm = [[storesMapView alloc] initWithNibName:@"storesMapView" bundle:nil]; 
    [svm initWithXML:stores:indexPath.row]; 
    CGRect theFrame = svm.view.frame; 
    theFrame.origin = CGPointMake(self.view.frame.size.width, 0); 
    svm.view.frame = theFrame; 
    theFrame.origin = CGPointMake(0,0); 
    theFrame.size=CGSizeMake(320,355); 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.3f]; 
    svm.view.frame = theFrame; 
    [UIView commitAnimations]; 
    [subView addSubview:svm.view]; 
    backButton.hidden=NO; 


} 
+0

Si dovrebbe davvero costruire celle come questa in un pennino, piuttosto che creare tutto in cellForRow. – Mark

+0

Puoi vedere la risposta anche qui.Chiaramente spiegato. http://stackoverflow.com/questions/8999300/labels-in-custom-cell-overlaps – user3675974

+0

Ho lo stesso problema, ma i miei subviews non sono istanziati in codice, ma vengo da uno storyboard. se un rimuovere i subviews sono tutti andati. Come posso risolvere questo? – DamirDiz

risposta

19

ho capito con alcuni tentativi ed errori; se questo può aiutare qualcuno

In cellForRowAtIndexPath ho provato a cancellare tutte le celle di visualizzazione secondaria prima di disegnare la cella

//TRY TO REMOVE ALL CONTENT 
    for(UIView *view in cell.contentView.subviews){ 
     if ([view isKindOfClass:[UIView class]]) { 
      [view removeFromSuperview]; 
     } 
    } 

sarei felice se qualcuno mi può puntare a un modo più semplice

Grazie

+0

Soluzione piacevole .... !! – TheTiger

+1

Il modo più semplice è quello di posizionare tutte le viste aggiunte dinamicamente in una sottoview comune e rimuovere tutte le sottoview di questa vista nel metodo prepareForReuse di UITableViewCell –

+0

@ Il suggerimento di NikolayKasyanov sopra è il posto giusto per farlo. – Echelon

7

È possibile utilizzare

[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)]; 

Se la cella a non pari a zero, il codice di cui sopra ridurrà il tempo per l'utilizzo del ciclo for simile a questo

for(UIView *eachView in [cell subviews]) 
    [eachView removeFromSuperview]; 
1

So che questo è un po 'tardi, ma ho avuto un problema simile in cui UILabels create per un le celle erano ancora parte della cella quando è stata riutilizzata. Quindi ogni successivo aggiornamento di tableview ha creato un altro UILabel in cima a quello esistente. Ho spostato la creazione delle etichette nella condizione if, come di seguito, e ho risolto il problema. Spero che aiuti qualcun altro. Inoltre, non noti alcuna versione mentre sto usando ARC.

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 

    { 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    cityText = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)]; 
    cityText.font = [UIFont fontWithName:@"Arial" size:20]; 
    cityText.textAlignment = UITextAlignmentLeft; 
    cityText.backgroundColor = [UIColor clearColor]; 

    regionText = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, 100, 20)]; 
    regionText.font = [UIFont fontWithName:@"Arial" size:20]; 
    regionText.textAlignment = UITextAlignmentLeft; 
    regionText.backgroundColor = [UIColor clearColor]; 

    } 
5

Ho anche avuto la stessa issue.I anche aveva composto tableView con labels.And quando ho scorrere verso il basso il contenuto ottenuto overlapped.But ha ottenuto risolto semplicemente modificando una dichiarazione nella cellForRowAtIndexPath.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 

Penso che questo risolva il problema.

+0

Questo codice funziona per me. – anjaly

0

Impostazione nil per etichettare il testo su sottoclasse 's metodo UITableViewCell prepareForReuse() Ha risolto il problema -

override func prepareForReuse() { 
    super.prepareForReuse() 
    self.label1.text = nil 
    self.label2.text = nil 
    .... 
} 

in comune se aiuta nessuno!

Problemi correlati