2015-04-21 24 views
5

Mam tableView z custom cell. Mam możliwość zapisania do ulubionych niektórych elementów, a kiedy to się dzieje, chcę dodać gwiazdkę image w cell. Próbowałem to zrobić, ale po pojawieniu się gwiazdy mam problem. Myślę, że to z powodu reusablecell, ale nie wiem, jak to rozwiązać. Mój problem polega na:stars appear again on the other cells even if the word is not added on favorites.Komórka niestandardowa wielokrotnego użytku

- (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; 

} 

Odpowiedz

2

Wymień następujący kod w miejscu cellForRowAtIndexPath. otrzymasz wynik pożądania.

- (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; 

} 

Mam nadzieję, że ci to pomoże.

+0

daj mi znać, jeśli masz problem. –

1

Trzeba usunąć obraz, jeśli obiekt nie jest TRUE

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

Tak, masz rację, że to ze względu na ponowne wykorzystanie komórek poprzez dequeueReusableCell z identyfikatorem jako-

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

zgodnie z twoją odpowiedzią quirements ustawić gwiazda zdjęcie na komórkę, aby wskazać jakieś ulubione elementy na odpowiedniej komórki, jak to

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

Kiedy każda komórka z gwiazdą obrazu jest wykorzystywany niż powinno zostać usunięte, jeśli kolejna komórka nie jakieś ulubione elementy ale jeśli to ma jakieś ulubione elementy niż powinien on być stosowany as-

Aby rozwiązać ten problem wystarczy dodać inny przypadek z powyższym, jeżeli oświadczenie

if (isTheObjectThere == TRUE) 
    cell.favImg.image=[UIImage imageNamed:@"[email protected]"]; 
else 
    cell.favImg.image=nil; 
Powiązane problemy