[英]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.