[英]Search in a tableView Cell issue
我正在使用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或类似的东西,以避免更多的编码。 如果有人熟悉它,请在这里分享。 非常感谢高级。
除了向显示项目添加字符串外,还添加包含有关国家/地区信息的对象。
[displayItems addObject:string];
在这里,您仅将countryName添加到displayItems中,但是在调用cellForRowAtIndexPath时:您要查询其大陆,国家/地区名称和大写字母,而在搜索后重新加载表格时,它们不在数组(displayItems)中。
我想你加入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];
}
崩溃时您究竟得到了哪个错误?
似乎是在将对象添加到displayItems
数组时。 您只需添加字符串对象,而不是国家对象。
您的currentCountry
不仅是string
不是country
对象。
因此,在您的代码中输入:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
当您尝试解析时,它无法达到:
大陆
国家的名字
首都
搜索详细对象时请小心。
当您搜索时,添加字符串以显示不是CountryInfo对象的项目,因此当再次调用cellforrow atindexpath时,它必须崩溃
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.