在tableView中搜索单元格问题

我来

我正在使用xcode 5,并且想要搜索tableView数据。我将“搜索栏和搜索显示控制器”用于“搜索”,将“ CustomCell”用于“ tableCell”,并且所有数据都是通过NSXMLParsing从远程服务器进行解析的。所有数据都以其相应的图像显示,没有任何问题,但是我的搜索选项无法正常工作。我对单阵列搜索使用相同的代码,但仍有效。但是这里不是。在搜索时它已崩溃。这是我的代码:

-(void)loadData
{
    countryParser = [[CountryParser alloc] loadXMLByURL:@"http://www.avowstudio.com/iOS/PartProject/iOS7/XMLParsingWithCustomeCell/XMLFiles/XMLParsingWithCustomCell.xml"];

    countryArray = [countryParser countryArray];

    displayItems = [[NSMutableArray alloc] initWithArray:countryArray];

    [self.countryTableView reloadData];
    [activityIndicator stopAnimating];
    [self loadArray];
}



-(void) loadArray
{
    CountryInfo *currentCountryOne = [[CountryInfo alloc] init];
    totalArray = [[NSMutableArray alloc] init];

    for (int i=0; i < [countryArray count]; i++)
    {
        currentCountryOne = [countryArray objectAtIndex:i];
        [totalArray addObject:currentCountryOne.countryName];
    }
}


-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([searchText length] == 0)
    {
        [displayItems removeAllObjects];
        [displayItems addObjectsFromArray:countryArray];
    }
    else
    {
        [displayItems removeAllObjects];
        displayItems = [[NSMutableArray alloc] init];

        for (NSString *string in totalArray)
        {
            NSRange stringRang = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (stringRang.location != NSNotFound)
            {
                [displayItems addObject:string];
            }
        }
    }
    [self.countryTableView reloadData];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [displayItems count];
}

// This method is kept the "tableRow height" after "done searching"
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    tableView.rowHeight = 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CountryCustomCell *Cell = [self.countryTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!Cell)
    {
        Cell = [[CountryCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    currentCountry = [displayItems objectAtIndex:indexPath.row];

    Cell.ContinentLabel.text = currentCountry.continent;
    Cell.CountryNameLabel.text = currentCountry.countryName;
    Cell.CapitalLabel.text = currentCountry.capital;

    imageQueueCountryFlag = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(imageQueueCountryFlag, ^
    {
        UIImage *imageCountryFlag = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentCountry countryFlag]]]];
    dispatch_async(dispatch_get_main_queue(), ^
        {
            Cell.CountryFlagImageView.image = imageCountryFlag;
            [Cell setNeedsLayout];
        });
    });

return Cell;

}

我不想在带有“标志”之类的“ numberOfRowsInSection”和“ cellForRowAtIndexPath”中使用两个Array或类似的东西,以避免更多的编码。如果有人熟悉,请在这里分享。非常感谢高级。

手工制作

我认为你添加的对象CountryInfo[countryParser countryArray] loadArray方法中,您只需初始化一个空白对象currentCountryOne,并在每次执行for循环时,尝试将空白对象的属性添加到totalArray中。请进行以下更改:

-(void) loadArray
{
    totalArray = countryArray;
}


-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([searchText length] == 0)
    {
        [displayItems removeAllObjects];
        [displayItems addObjectsFromArray:countryArray];
    }
    else
    {
        [displayItems removeAllObjects];
        displayItems = [[NSMutableArray alloc] init];

        for (CountryInfo *country in totalArray)
        {
            NSRange stringRang = [country.countryName rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (stringRang.location != NSNotFound)
            {
                [displayItems addObject:country];
            }
        }
    }
    [self.countryTableView reloadData];
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在TableView中编辑数字单元格

来自分类Dev

静态tableview中的单元格内容未显示

来自分类Dev

在iOS的TableView中的选定单元格上设置ImageView的问题

来自分类Dev

iOS TableView在每个单元格中显示相同的对象

来自分类Dev

TableView中的可编辑多行单元格

来自分类Dev

如何在TableView中复制/粘贴表格单元格

来自分类Dev

在TableView单元格中快速显示解析图像

来自分类Dev

UISearchController搜索栏在tableview中隐藏第一个单元格

来自分类Dev

在单元格中搜索术语

来自分类Dev

在单元格中搜索字符列表

来自分类Dev

动态Tableview单元格

来自分类Dev

在某些tableView单元格中快速更改单元格背景颜色

来自分类Dev

使用自定义单元格从搜索更新tableView

来自分类Dev

关于tableview单元格和segue的奇怪问题

来自分类Dev

在tableView中搜索单元格问题

来自分类Dev

在TableView中编辑数字单元格

来自分类Dev

创建tableView单元格

来自分类Dev

openpyxl中的单元格公式问题

来自分类Dev

在Excel中搜索单元格

来自分类Dev

在Matlab的单元格数组中搜索

来自分类Dev

在Excel中搜索重复的单元格

来自分类Dev

搜索结果未在tableView中显示单元格

来自分类Dev

在tableView中获取单元格值

来自分类Dev

在单元格中搜索术语

来自分类Dev

Tableview单元格覆盖

来自分类Dev

Tableview单元格偏移

来自分类Dev

TableView 单元格操作

来自分类Dev

如何在tableview中的单元格之间放置临时单元格?

来自分类Dev

tableView 滚动到最新单元格的底部 - scrollToRow() 的问题