当我开始在uitableview中输入uiseachbar时,应用程序崩溃了

时间:2015-06-22 06:01:12

标签: ios

我正在制作一个项目,我在NSObject课程的帮助下在我的tableview中获取地址簿。

现在,我在其中分配了UISearchbar,但是当我开始输入时,应用程序崩溃并出现以下错误:---- '无法在集合中使用/ contains运算符(不是一个集合)'。

我的代码

-(void)loadContacts { 
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

   if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
       ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
          ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
          //NSLog(@"%@", addressBook);
      });
   } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
       CFErrorRef *error = NULL;
       ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
       CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
       CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

       for(int i = 0; i < numberOfPeople; i++) {
           ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
           NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
           NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));

           ContactData *dataObj = [[ContactData alloc]init];
           NSString *Name = @" ";

           if ([firstName length]>0) {
               if ([lastName length]>0) {
                   Name = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
               } else {
                   Name = firstName;
               }
           } else {
               Name = @" ";
           }
           ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
           [[UIDevice currentDevice] name];
           //NSLog(@"\n%@\n", [[UIDevice currentDevice] name]);

           NSString *phoneNumber = @" ";
           for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
              phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
           }

           ABMultiValueRef Emails = ABRecordCopyValue(person, kABPersonEmailProperty);
           [[UIDevice currentDevice] name];
           NSString *Email; //= @" ";
           for (CFIndex i = 0; i < ABMultiValueGetCount(Emails); i++) {
               Email = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(Emails, i);

           }
           ABMultiValueRef Addresses = ABRecordCopyValue(person, kABPersonAddressProperty);

           NSString *address = @" ";
           NSMutableDictionary* addressDict = [[NSMutableDictionary alloc]init];

           for (CFIndex i = 0; i < ABMultiValueGetCount(Addresses); i++) {
               addressDict = (__bridge_transfer NSMutableDictionary *) ABMultiValueCopyValueAtIndex(Addresses, i);

               //NSLog(@"address = %@", addressDict);

               NSString * street = [addressDict objectForKey:@"Street"];
               NSString * city = [addressDict objectForKey:@"City"];
               NSString * state = [addressDict objectForKey:@"State"];
               NSString *country = [addressDict objectForKey:@"Country"];


              if (country.length>0 || state.length>0 || city.length>0 || street.length>0) {

                   if (country.length>0 && state.length>0) {
                       address = [NSString stringWithFormat:@"%@, %@", state,country];
                   } else if(country.length>0 && city.length>0){
                       address = [NSString stringWithFormat:@"%@, %@", city,country];
                   } else if (state.length>0 && street.length>0){
                       address = [NSString stringWithFormat:@"%@, %@", street,country];
                   } else if (state.length>0 && city.length>0){
                       address = [NSString stringWithFormat:@"%@, %@", city,state];
                   } else if (state.length>0 && street.length>0) {
                       address = [NSString stringWithFormat:@"%@, %@", street,state];
                   } else if (city.length>0 && street.length>0) {
                       address = [NSString stringWithFormat:@"%@, %@", street,city];
                   } else if (country.length>0) {
                       address = country;
                   } else if (state.length>0) {
                       address = state;
                   } else if (city.length>0) {
                       address = city;
                   }
               } else {
                   address = @" ";
               }
           }

           //NSLog(@"address = %@", address);

           NSData *imgData = (__bridge_transfer NSData *) ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);

           dataObj.name = Name;
           dataObj.email = Email;
           dataObj.phone = phoneNumber;
           dataObj.imageData = imgData;
           dataObj.address = address;

           [contactArr addObject:dataObj];
       }
       NSLog(@"contacts loaded");
       //NSLog(@"count = %lu", (unsigned long)contactArr.count);
  } else {
       //Go to settings and allow access to use contacts[GlobalFunction removeWaitView];
  }

搜索栏的代码是

     - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
      {

        NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
        searchResults = [contactArr filteredArrayUsingPredicate:resultPredicate];
        }


        -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
        {
         [self filterContentForSearchText:searchString scope:   [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
           return YES;

}

2 个答案:

答案 0 :(得分:1)

似乎谓词不正确 试试:

   NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.fullName contains[c] %@", searchText];

答案 1 :(得分:0)

这是cellForRowAtIndexPath

  -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
static NSString *cellIdentifier=@"CellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell==nil)
  {

   cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
   cell.backgroundColor=[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:239.0/255.0 alpha:1.0];

}


if (tableView == self.searchDisplayController.searchResultsTableView) {
    personContact = [searchResults objectAtIndex:indexPath.row];

}
else {
    personContact = [contactsArr objectAtIndex:indexPath.row];

}

  cell.textLabel.text = personContact.fullName;

return cell;

}