简体   繁体   English

在tableView中搜索单元格问题

[英]Search in a tableView Cell issue

I am using xcode 5 and want to search a tableView data. 我正在使用xcode 5,并且想要搜索tableView数据。 I used "Search Bar & Search Display Controller" for "searching", "CustomCell" for "tableCell" and all the data are parsing from a remote server by NSXMLParsing. 我将“搜索栏和搜索显示控制器”用于“搜索”,将“ CustomCell”用于“ tableCell”,并且所有数据都是通过NSXMLParsing从远程服务器进行解析的。 All data are showing with their corresponding image without any problem but some how my search option doesn't work properly. 所有数据都以其相应的图像显示,没有任何问题,但是我的搜索选项无法正常工作。 I do the same code for a single array searsing and it was working. 我对单阵列搜索进行了相同的代码,并且可以正常工作。 But here it isn't. 但是这里不是。 At the time of searching it is crashed. 在搜索时它已崩溃。 Here is my code: 这是我的代码:

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

} }

I don't want to use two Array in "numberOfRowsInSection" & "cellForRowAtIndexPath" with flag or some thing like that, to avoid more coding. 我不想在带有“标志”之类的“ numberOfRowsInSection”和“ cellForRowAtIndexPath”中使用两个Array或类似的东西,以避免更多的编码。 If any one familiar with it please share that here. 如果有人熟悉它,请在这里分享。 A lot of thanks in advanced. 非常感谢高级。

Instead of adding string to displayItems add the objects containing information about country. 除了向显示项目添加字符串外,还添加包含有关国家/地区信息的对象。

[displayItems addObject:string]; [displayItems addObject:string];

Here you are adding only the countryName to displayItems but while calling cellForRowAtIndexPath: you are asking for its continent,country name and capital which is not present in the array (displayItems) when you reload table after searching. 在这里,您仅将countryName添加到displayItems中,但是在调用cellForRowAtIndexPath时:您要查询其大陆,国家/地区名称和大写字母,而在搜索后重新加载表格时,它们不在数组(displayItems)中。

I think you are added objects of CountryInfo in the [countryParser countryArray] . 我想你加入CountryInfo的对象在[countryParser countryArray。 In your loadArray method, you just initializing a blank object currentCountryOne and each time the for loop executes, trying to add a blank object's property into totalArray. loadArray方法中,您只需初始化一个空白对象currentCountryOne,并在每次执行for循环时,尝试将空白对象的属性添加到totalArray中。 Please make changes as below: 请进行以下更改:

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

Exactly which error did you get when it crashed? 崩溃时您究竟得到了哪个错误?

It seems to be that when you add an object to your displayItems array. 似乎是在将对象添加到displayItems数组时。 You just add the string object, not the country object. 您只需添加字符串对象,而不是国家对象。

Your currentCountry is not a country object just the string . 您的currentCountry不仅是string不是country对象。

So, in your code enter: 因此,在您的代码中输入:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

When you try to parse, it cannot reach: 当您尝试解析时,它无法达到:

continent 大陆

countryName 国家的名字

capital 首都

Be careful when you search a detailed object. 搜索详细对象时请小心。

当您搜索时,添加字符串以显示不是CountryInfo对象的项目,因此当再次调用cellforrow atindexpath时,它必须崩溃

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM