【发布时间】:2013-11-06 01:00:01
【问题描述】:
我是 Objective C 的新手,这是我的问题。我有两个值的数组, 整数和字符串:
[Messier new:1 :@"Name 1"],
[Messier new:2 :@"Name 2"]...
我将这些数组推送到我的tableView 中,在tableView 中有UISearchBar。当我点击单元格时,我创建文本按钮“显示”
我需要从我的数组中返回所选名称的 int 值(单元格中的名称)。在正常状态下(搜索前)我的按钮返回正确的值,但搜索后返回错误。例如(看上图)仙女座有数字 11,蝴蝶 20,蜂巢 30,我正在搜索蜂巢(数字 30),搜索后(蜂巢获取第一个单元格)我得到值 11(它是仙女座值),而不是 30根据我的需要。
这是我的 serchBar 代码:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if([searchText isEqualToString:@""] || searchText == Nil)
{
searching = NO;
}
else
{
searching = YES;
[searchArray removeAllObjects];
searchArray = [[NSMutableArray alloc] init];
searchDictionary = [[NSMutableDictionary alloc] init];
NSArray *sourceArray;
NSDictionary *sourceDictionary;
sourceArray = [[AstroData sharedAstroData] messierObjectsArray];
sourceDictionary = [[AstroData sharedAstroData] messierNameIndexesDictionarry];
for(NSString *key in [sourceDictionary allKeys])
{
NSArray *messier = [sourceDictionary objectForKey:key];
for(Messier *mess in messier)
{
NSRange result = [mess.name rangeOfString:searchText options:(NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch)];
if(result.location != NSNotFound)
{
NSMutableArray *existingArray;
if((existingArray = [searchDictionary objectForKey:key]))
{
[existingArray addObject:mess];
}
else
{
NSMutableArray *tempArray = [NSMutableArray array];
[searchDictionary setObject:tempArray forKey:key];
[tempArray addObject:mess];
}
}
}
}
for(id key in sourceArray)
{
if([searchDictionary objectForKey:key])
[searchArray addObject:key];
}
}
[table reloadAllComponents];
}
也许我的表格视图有问题?谢谢大家。
附:对不起我的英语)
【问题讨论】:
标签: ios uitableview uisearchbar