我在尝试弄清楚如何在UItableview上显示我过滤的搜索结果时遇到了很多麻烦。
我的显示方法效果非常好......但是当我尝试在数组中搜索特定数据时,它只会显示FIRST OBJECT OF THE SECTIONS
或崩溃,并显示以下错误消息:
*由于未捕获的异常'NSRangeException'而终止应用程序,原因:' - [__ NSArrayI objectAtIndex:]:索引2超出边界[0 .. 0]'*
我的数组结构:AllDataGroupbyServiceArray
(
{
Companies = (
{
AddressInformation = h3k2w3;
Name = Teg;
Service = "";
}
);
Services = "";
},
{
Companies = (
{
AddressInformation = j9g2k6;
Name = Megan;
Service = Electrician;
},
{
AddressInformation = h2k2w3;
Name = test;
Service = Electrician;
},
{
AddressInformation = h2t3b3;
Name = Janet;
Service = Electrician;
},
{
AddressInformation = "J4J 1H7";
Name = Rick;
Service = Electrician;
},
{
AddressInformation = "H1T 4B6";
Name = Herber;
Service = Electrician;
},
{
AddressInformation = "J0K 3B0";
Name = test;
Service = Electrician;
},
{
AddressInformation = h2k2w3;
Name = test;
Service = Electrician;
},
{
AddressInformation = h2k2w3;
Name = canada;
Service = Electrician;
}
);
Services = Electrician;
},
{
Companies = (
{
AddressInformation = J0L2K0;
Name = "Colas Bn";
Service = Mason;
},
{
AddressInformation = J5A1M2;
Name = mrnoskill2;
Service = Mason;
},
{
AddressInformation = J0L2K0;
Name = test8;
Service = Mason;
},
{
AddressInformation = J5C1S3;
Name = test6;
Service = Mason;
},
{
AddressInformation = h3r1z2;
Name = Jason;
Service = Mason;
},
{
AddressInformation = j7h2k7;
Name = max;
Service = Mason;
},
{
AddressInformation = J5Z2W5;
Name = Fred;
Service = Mason;
},
{
AddressInformation = "H4G 1K6";
Name = Joe;
Service = Mason;
},
{
AddressInformation = j3y7a6;
Name = Tets;
Service = Mason;
}
);
Services = Mason;
},
{
Companies = (
{
AddressInformation = h2k2w3;
Name = jake;
Service = Plumber;
}
);
Services = Plumber;
}
)
以下是过滤方法:
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[filteredObject removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY %K.%K CONTAINS[cd] %@",
@"Companies",@"Name",searchText];
filteredObject = [NSMutableArray arrayWithArray:[AllDataGroupbyServiceArray filteredArrayUsingPredicate:predicate]];
}
部分代码行数:效果不错但搜索不行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Returns the number of items in the array associated with the letter for this section.
// Check to see whether the normal table or search results table is being displayed and return the count from the appropriate array
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSArray *filterCount = [filteredObject valueForKey:@"Services" ];
return [filterCount count];
} else {
NSString* serviceSection = [publicServiceArray objectAtIndex:section];
NSArray* arrayservicescount = (NSArray*)[ServicesDictionary objectForKey:serviceSection];
return arrayservicescount.count;
}
}
表标题:效果很好
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// Condition for the search tableview
if( tableView == self.searchDisplayController.searchResultsTableView)
{
NSArray *sections = [NSArray array] ;
sections = [ filteredObject valueForKey:@"Services"] ;
return [ sections objectAtIndex:section] ;
}
NSArray *sections = [NSArray array] ;
sections = [[myObject valueForKeyPath:@"@distinctUnionOfObjects.Service"]sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] ;
return [sections objectAtIndex:section];
}
单元格显示问题:不适用于搜索
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell=[[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSMutableDictionary *tmpDict = [myObject objectAtIndex:AllDataGroupbyServiceArray.count];
// set the search object from the appropriate array
// Condition for the search tableview
if (tableView == self.searchDisplayController.searchResultsTableView) {
tmpDict = [[filteredObject valueForKey:@"Companies" ]objectAtIndex:indexPath.row]
} else {
tmpDict = [[[AllDataGroupbyServiceArray valueForKey:@"Companies" ] objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] ;
}
NSMutableString *text;
text = [tmpDict valueForKey:name] ;
NSMutableString *detail;
detail =[NSMutableString stringWithFormat:@"Postal code : %@ ", [tmpDict valueForKey:serviceAddress]];
// NSMutableString *images;
// images = [NSMutableString stringWithFormat:@"%@ ",
// [tmpDict objectForKey:serviceAddress]];
// NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
// NSData *data = [NSData dataWithContentsOfURL:url];
// UIImage *img = [[UIImage alloc]initWithData:data];
cell.textLabel.text = text ;
cell.detailTextLabel.text= detail ;
cell.imageView.frame = CGRectMake(0, 0, 80, 70);
// cell.imageView.image =img;
return cell;
}
我知道我非常接近答案,但任何帮助都会受到赞赏
提前致谢
答案 0 :(得分:0)
这行似乎是一个问题
你已经有了一行带有此行的filterObject
filteredObject = [NSMutableArray arrayWithArray:[AllDataGroupbyServiceArray filteredArrayUsingPredicate:predicate]];
再次为服务键
过滤此数组if (tableView == self.searchDisplayController.searchResultsTableView) {
NSArray *filterCount = [filteredObject valueForKey:@"Services" ];
*您应该返回filteredObject count *
return [filterCount count];
}