2015-04-21 19 views
5

Ho un tableView con un custom cell. Ho la possibilità di salvare tra i preferiti alcuni elementi e, quando ciò accade, voglio aggiungere una stella image in cell. Stavo provando a farlo, ma dopo che la stella appare, ho un problema. Penso che sia a causa di reusablecell ma non so come risolverlo. Il mio problema è:stars appear again on the other cells even if the word is not added on favorites.Cella riutilizzabile personalizzata

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    dictionaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (!cell) { 
     cell=[[dictionaryTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; 
    } 
    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row]; 

    } 
    else 
    { 
     cell.word.text = self.tableData[indexPath.row]; 
     BOOL isTheObjectThere = [self.favoriteArry containsObject:self.tableData[indexPath.row]]; 
     if (isTheObjectThere==TRUE) { 
      cell.favImg.image=[UIImage imageNamed:@"[email protected]"]; 
     } 
    } 

     return cell; 

} 

risposta

2

Sostituire seguente codice al posto di cellForRowAtIndexPath. otterrete il vostro desiderio di uscita.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    dictionaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (!cell) { 
     cell=[[dictionaryTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; 
    } 
    cell.favImg.hidden = YES; 
    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row]; 

    } 
    else 
    { 
     cell.word.text = self.tableData[indexPath.row]; 
     BOOL isTheObjectThere = [self.favoriteArry containsObject:self.tableData[indexPath.row]]; 
     if (isTheObjectThere==TRUE) { 
      cell.favImg.hidden = NO; 
      cell.favImg.image=[UIImage imageNamed:@"[email protected]"]; 
     } 
    } 

    return cell; 

} 

Spero che questo ti aiuti.

+0

fatemi sapere se avete problemi. –

1

è necessario rimuovere l'immagine, se l'oggetto non è TRUE

if (isTheObjectThere==TRUE) { 
    cell.favImg.image=[UIImage imageNamed:@"[email protected]"]; 
} else { 
    cell.favImg.image=nil; 
} 
1

Sì, hai ragione è a causa di riutilizzare le cellule tramite dequeueReusableCell con identificatore as-

dictionaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

secondo il vostro re genze di impostare un'immagine di stella su una cella per indicare alcuni elementi preferiti sulla rispettiva cella, come questo

BOOL isTheObjectThere = [self.favoriteArry containsObject:self.tableData[indexPath.row]]; 
     if (isTheObjectThere==TRUE) { 
      cell.favImg.image=[UIImage imageNamed:@"[email protected]"]; 
     } 

Quando una cella di immagine stelle con viene riutilizzato di quello che dovrebbe essere rimosso se la cella successiva non lo fa alcuni elementi preferiti ma se ha alcuni elementi preferiti di quello che dovrebbe essere utilizzato as-

Per risolvere questo problema è sufficiente aggiungere il caso altro con quanto sopra if come

if (isTheObjectThere == TRUE) 
    cell.favImg.image=[UIImage imageNamed:@"[email protected]"]; 
else 
    cell.favImg.image=nil; 
Problemi correlati