2013-10-07 21 views
1

我正在使用xcode 5并想要搜索tableView数据。我使用“搜索栏&搜索显示控制器”进行“搜索”,“CustomCell”用于“tableCell”,所有数据都通过NSXMLParsing从远程服务器解析。所有数据都显示与他们的相应图像没有任何问题,但一些如何我的搜索选项无法正常工作。我为单个数组执行相同的代码,并且它正在工作。但这不是。在搜索时它崩溃了。这里是我的代码:在tableView中搜索单元问题

-(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”使用两个磁盘阵列和标志或类似的一些事情,以避免更多的代码。如果有人熟悉它,请在此分享。先进的很多感谢。

+0

你可以打印totalArray – manujmv

回答

2

我觉得你在国家信息添加对象[countryParser countryArray]。在你loadArray方法,你只是初始化一个空对象currentCountryOne每一次的循环执行,试图空白对象的属性添加到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]; 
} 
+0

非常感谢@manujmv :),其实我在想它,但是对代码形成感到困惑。它完美的工作和我的愿望解决方案。万岁兄弟。 :) 我必须比较我的搜索与“CountryInfo”对象不与字符串。而已。 – Tulon

0
当你搜索你添加字符串显示项目

不CountryInfo对象,因此当cellforrow atindexpath被再次调用它崩溃

+0

是的,我明白了你的观点。我在“manujmv”的帮助下解决了这个问题。感谢评论:) – Tulon

2

而不是添加字符串到displayItems添加包含有关国家信息的对象。

[displayItems addObject:string];

在这里,您只将countryName添加到displayItems,但在调用cellForRowAtIndexPath时:当您搜索后重新加载表时,您正在询问其数据库中没有的大陆,国家名称和大写字母(displayItems)。

+0

是的,我明白你的观点。我在“manujmv”的帮助下解决了这个问题。感谢评论:) – Tulon

1

确切地说,当它坠毁时会得到哪个错误?

这似乎是,当你添加一个对象到您的displayItems阵列。您只需添加字符串对象,而不是国家对象。

您的currentCountry不是country对象只是string

所以,在你的代码输入:

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

当你尝试分析,不能达到:

大陆

国家名称

资本

当您搜索详细的对象时请小心。