Objective c iOS搜索栏问题
我正在搜索表视图数据。Tableview包含联系人列表,当我给出联系人姓名时,它无法给出搜索栏结果。这是我的密码:Objective c iOS搜索栏问题,objective-c,uitableview,uisearchbar,uisearchdisplaycontroller,Objective C,Uitableview,Uisearchbar,Uisearchdisplaycontroller,我正在搜索表视图数据。Tableview包含联系人列表,当我给出联系人姓名时,它无法给出搜索栏结果。这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; // Hide the search bar until user scrolls up CGRect newBounds = [[self tableview] bounds]; newBounds.origin.y = newBounds.origin.y
- (void)viewDidLoad
{
[super viewDidLoad];
// Hide the search bar until user scrolls up
CGRect newBounds = [[self tableview] bounds];
newBounds.origin.y = newBounds.origin.y + search.bounds.size.height;
[[self tableview] setBounds:newBounds];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(goToSearch)];
self.navigationItem.rightBarButtonItem=doneButton;
self.filtereditems=[NSMutableArray arrayWithCapacity:[array count]];
[self loadPhoneContacts];
[[self tableview] reloadData];
}
- (void)goToSearch
{
[search becomeFirstResponder];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:
[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(void)loadPhoneContacts{
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status == kABAuthorizationStatusDenied) {
[[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone
Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
return;
}
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (error) {
NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
if (addressBook) CFRelease(addressBook);
return;
}
if (status == kABAuthorizationStatusNotDetermined)
{
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@",
CFBridgingRelease(error));
}
if (granted) {
// if they gave you permission, then just carry on
[self listPeopleInAddressBook:addressBook];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to
your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone
Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
});
}
if (addressBook) CFRelease(addressBook);
});
} else if (status == kABAuthorizationStatusAuthorized) {
[self listPeopleInAddressBook:addressBook];
if (addressBook) CFRelease(addressBook);
}
}
- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray * contactList=
CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
array =[[NSMutableArray alloc] init];
for (int i = 0; i < numberOfPeople; i++)
{
ABRecordRef person = (__bridge ABRecordRef)contactList[i];
firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSLog(@"Name:%@ %@", firstName, lastName);
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
for (CFIndex i = 0; i < numberOfPhoneNumbers; i++)
{
phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
NSLog(@" phone is:%@", phoneNumber);
}
[array addObject:[NSDictionary dictionaryWithObjectsAndKeys:firstName , @"first_name"
, lastName , @"last_name" ,phoneNumber,@"phone_Number" ,nil]];
NSArray *ar=[[NSArray alloc]init];
NSLog(@"array is,%@",ar);
CFRelease(phoneNumbers);
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
return [filtereditems count];
else
return [array count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
NSLog(@"array is %@",array);
static NSString *CustomCellID = @"cell";
contactcellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellID];
if (cell == nil)
{
cell=[[contactcellTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CustomCellID];
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"contactcellTableViewCell" owner:self
options:nil];
cell =[nib objectAtIndex:0];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.Firstnamelbl.text = [filtereditems objectAtIndex:indexPath.row];
cell.Lastnamelbl.text = [filtereditems objectAtIndex:indexPath.row];
} else {
cell.Firstnamelbl.text = [[array
objectAtIndex:indexPath.row]objectForKey:@"first_name"];
cell.Lastnamelbl.text = [[array objectAtIndex:indexPath.row]objectForKey:@"last_name"];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
[array removeAllObjects];
contactcellTableViewCell *cell = (contactcellTableViewCell *) [tableView
cellForRowAtIndexPath:indexPath];
static NSString *CustomCellID = @"cell";
NSString *string=[firstName stringByAppendingString:lastName];
if (cell == nil)
{
cell=[[contactcellTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CustomCellID];
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"contactcellTableViewCell" owner:self
options:nil];
cell =[nib objectAtIndex:0];
}
if (cell.m_checkImageView.image == [UIImage imageNamed:@"Selected.png"])
{
cell.m_checkImageView.image = [UIImage imageNamed:@"Unselected.png"];
}
else
{
cell.m_checkImageView.image = [UIImage imageNamed:@"Selected.png"];
NSLog(@"string is %@",string);
}
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filtereditems removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c]
%@",searchText];
NSArray *tempArray = [array filteredArrayUsingPredicate:predicate];
if(![scope isEqualToString:@"All"]) {
// Further filter the array with the scope
NSPredicate *scopePredicate = [NSPredicate predicateWithFormat:@"SELF.category
contains[c] %@",scope];
tempArray = [tempArray filteredArrayUsingPredicate:scopePredicate];
}
filtereditems = [NSMutableArray arrayWithArray:tempArray];
}
-(void)viewDidLoad
{
[超级视图下载];
//隐藏搜索栏,直到用户向上滚动
CGRect新边界=[[self tableview]边界];
newBounds.origin.y=newBounds.origin.y+search.bounds.size.height;
[[self tableview]设置边界:新边界];
UIBarButtonItem*doneButton=[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItem目标:自我操作:@selector(goToSearch)];
self.navigationItem.RightBarButtonim=doneButton;
self.filtereditems=[NSMutableArray arrayWithCapacity:[array count]];
[自动加载电话联系人];
[[self tableview]重新加载数据];
}
-(无效)goToSearch
{
[搜索成为第一响应者];
}
-(BOOL)searchDisplayController:(UISearchDisplayController*)控制器
shouldReloadTableForSearchString:(NSString*)searchString
{
//通知表格数据源在文本更改时重新加载
[self-filterContentForSearchText:searchString作用域:
[[self.searchDisplayController.searchBar ScopeButtonTiles]对象索引:
[self.searchDisplayController.searchBar selectedScopeButtonIndex]];
//返回YES以重新加载搜索结果表视图。
返回YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController*)控制器
应重新加载搜索范围的表:(NSInteger)搜索选项
{
//指示表数据源在范围栏选择更改时重新加载
[self-filterContentForSearchText:[self.searchDisplayController.searchBar text]范围:
[[self.searchDisplayController.searchBar ScopeButtonTiles]对象索引:搜索选项]];
//返回YES以重新加载搜索结果表视图。
返回YES;
}
-(void)加载电话联系人{
ABAuthorizationStatus=ABAddressBookGetAuthorizationStatus();
如果(状态==KAB授权状态拒绝){
[[[UIAlertView alloc]initWithTitle:nil message:@“此应用程序需要访问您的联系人才能正常运行。请访问iPhone中的“隐私”部分
设置应用程序。“代理:无取消按钮提示:@“确定”其他按钮提示:无]显示];
返回;
}
CFErrorRef error=NULL;
ABAddressBookRef addressBook=ABAddressBookCreateWithOptions(NULL,错误(&R);
如果(错误){
NSLog(@“ABAddressBookCreateWithOptions错误:%@”,CfBrigingRelease(错误));
如果(地址簿)发布(地址簿);
返回;
}
如果(状态==KAB授权状态未确定)
{
ABAddressBookRequestAccessWithCompletion(addressBook,^(已授予bool,CFErrorRef错误){
如果(错误){
NSLog(@“ABAddressBookRequestAccessWithCompletion错误:%@”,
CfBrigingRelease(错误));
}
如果(授予){
//如果他们允许你,那就继续吧
[self-listPeopleInAddressBook:addressBook];
}否则{
dispatch\u async(dispatch\u get\u main\u queue()^{
[[[UIAlertView alloc]initWithTitle:nil message:@“此应用程序需要访问
请访问iPhone中的“隐私”部分
设置应用程序。“代理:无取消按钮提示:@“确定”其他按钮提示:无]显示];
});
}
如果(地址簿)发布(地址簿);
});
}else if(状态==KAB授权状态授权){
[self-listPeopleInAddressBook:addressBook];
如果(地址簿)发布(地址簿);
}
}
-(作废)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
NSInteger numberOfPeople=ABAddressBookGetPersonCount(地址簿);
NSMUTABLEARRY*联系人列表=
CfBrigingRelease(ABAddressBookCopyArrayFallPeople(addressBook));
array=[[NSMutableArray alloc]init];
for(int i=0;i