可重复使用的自定义单元格

Adi*_*rin 5 objective-c cell uitableview ios

我有tableView一个custom cell.我有posibility保存到收藏夹中的一些内容,当发生这种情况我想补充一个明星imagecell.我试图这样做,但在明星出现后,我有一个问题.我认为这是因为reusable cell但我不知道如何解决它. 我的问题是: stars appear again on the other cells even if the word is not added on favorites.

- (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:@"3081@3x.png"];
        }
    }

        return cell;

}
Run Code Online (Sandbox Code Playgroud)

Jat*_* JP 2

将以下代码替换为cellForRowAtIndexPath. 你会得到你想要的输出。

- (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:@"3081@3x.png"];
        }
    }

    return cell;

}
Run Code Online (Sandbox Code Playgroud)

希望这对您有帮助。