0

我有一个表格视图,其中包含单元格图像和名称作为文本。在顶部,我添加了一个搜索栏。当我搜索时,我可以搜索名称,但图像仍然相同。如何根据搜索时名称的更改也更改图像?这段代码在 tableview 委托中,我也在使用一些搜索委托方法。我没有做任何事情来安排与名称相关的图像。我需要帮助来实现它。

if(searching)
        cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
    else {

        [cell.imageView setImageWithURL:[NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]  
                   placeholderImage:[UIImage imageNamed:@"defaultPerson.png"]];
        cell.textLabel.text = [friendsList objectAtIndex:indexPath.row];        
    }

这里还有一些代码:

- (void) searchTableView {

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];


    for (NSString *sTemp in friendsList)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
            [copyListOfItems addObject:sTemp];
    }

    [searchArray release];
    searchArray = nil;
}

谢谢。

4

1 回答 1

1

在我看来,您没有正确更新您的单元格。

一个简单[myTableView reloadData];的方法可能会有所帮助,但您可能还需要考虑研究将 imageViews 添加到单元格的方式。您可能没有正确删除(/释放/取消)您放置在单元格顶部的图像视图。每次更新单元格时都必须这样做,因为除非您明确告诉它们,否则 imageViews 不会自己执行此操作。

当您使用一些有用的代码更新您的问题时,我可以更具体地说明您的问题。


现在您确实使用一些(尽管数量很少)代码编辑了您的帖子,我可以从这个片段中看到您没有将 cell.imageViews 更新为新的对象列表。

您使用新的“copyListOfItems”更新了您的 uitableview,但您没有为您的图像做同样的事情,导致显示来自“imageArray”的图像的旧内容

您需要为图像创建一个与文本类似的数组。

if(searching)
{
    cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
    [cell.imageView setImageWithURL:[NSURL URLWithString:[searchedArrayOfImages objectAtIndex:indexPath.row]];  
}
else {

    [cell.imageView setImageWithURL:[NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]  
               placeholderImage:[UIImage imageNamed:@"defaultPerson.png"]];
    cell.textLabel.text = [friendsList objectAtIndex:indexPath.row];        
}

将图像保存到名称的更好方法(imo)是为它们制作一个 NSDictionary,因此当您选择记录时,您的名称总是适合您的图像。


关于下面的评论:在您搜索的那一刻,很可能在textDidChange委托方法中,您很可能会选择所有搜索匹配项的索引行。在您从“friendlist”中选择命中的索引并将其放入“copyListOfItems”时,您还需要使用相同的索引并从“imageArray”中获取图像并将它们放入“searchedArrayOfImages”。这样,您就可以以相同的方式构建文本和图像数组。然后,当您重新加载表格视图时,您可以从“copyListOfItems”和“searchedArrayOfImages”中获取相同的索引并正确填充您的单元格。


它看起来像这样:

- (void) searchTableView 
{

    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    int indexCount = 0; //keeps track of which index you are at.
    for (NSString *sTemp in friendsList)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
        {
            [copyListOfItems addObject:sTemp]; //this is your name result
            [searchedArrayOfImages addObject:[imageArray objectAtIndex:indexCount]]; //adds image that fits with the nameresult to array of searched images
        }

        indexCount++;
    }

    [searchArray release];
    searchArray = nil;
}

完成搜索后,单元格将像这样更新(根据您的代码段)

if(searching)
{
    cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
    [cell.imageView setImageWithURL:[NSURL URLWithString:[searchedArrayOfImages objectAtIndex:indexPath.row]];  
}
else 
{

    [cell.imageView setImageWithURL:[NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]  
               placeholderImage:[UIImage imageNamed:@"defaultPerson.png"]];
    cell.textLabel.text = [friendsList objectAtIndex:indexPath.row];        
}

这应该足够了^^祝你好运。

于 2011-10-20T11:20:16.360 回答