使用UISearchController时在UITableView中处理cellForRowAtIndexPath时遇到问题

时间:2017-01-06 10:46:14

标签: ios objective-c iphone uitableview

我在目标c中创建UISearchController。我从本地存储中获取联系人并在UITableView中显示它们。能够导入和显示值。我正在根据用户名搜索值,我将搜索到的值存储在NSArray中,并根据用户名显示搜索到的值。能够显示用户名,但我无法处理cellForRowAtIndexPath以显示特定用户的详细信息,如userthumbnail图像和用户phonenumber。

以下是我的代码

@interface SelectContactViewController (){
     NSMutableArray *contactFullNameArr;
      NSMutableArray *contactPhoneNumberArr;
      NSMutableArray *contactImgPathArr;

    NSArray *searchResultsArray;;
}

- (void)viewDidLoad {
    [super viewDidLoad];

contactFullNameArr = [[NSMutableArray alloc]init];
    contactPhoneNumberArr = [[NSMutableArray alloc]init];
    contactImgPathArr = [[NSMutableArray alloc]init];

 [self fetchContactsandAuthorization];

    searchResultsArray = [[NSArray alloc]init];

    self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
    self.searchController.searchBar.delegate = self;
    self.searchController.searchResultsUpdater = self;
    [self.searchController.searchBar sizeToFit];
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.definesPresentationContext = YES;
    _selectContactListTblView.tableHeaderView = self.searchController.searchBar;

    [_selectContactListTblView reloadData];


}


-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSString *searchString = self.searchController.searchBar.text;
    NSPredicate *resultPredicate;
        resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchString];
    searchResultsArray = [contactFullNameArr filteredArrayUsingPredicate:resultPredicate];
    [_selectContactListTblView reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
    [self updateSearchResultsForSearchController:self.searchController];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SelectContactTableViewCell";

    SelectContactTableViewCell *cell = (SelectContactTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SelectContactTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

     if(self.searchController.active){
              cell.contactFullName.text = [searchResultsArray objectAtIndex:indexPath.row];
//**************************i am confused here about setting the related user values to the cell************//
             cell.contactProfileImage.image = [contactImgPathArr objectAtIndex:indexPath.row];

         cell.contactPhoneNumber.text = [contactPhoneNumberArr objectAtIndex:indexPath.row];
     }
     else{
    cell.contactFullName.text = [contactFullNameArr objectAtIndex:indexPath.row];
    NSLog(@"img %@",[contactImgPathArr objectAtIndex:indexPath.row]);
    cell.contactProfileImage.image = [contactImgPathArr objectAtIndex:indexPath.row];

    cell.contactPhoneNumber.text = [contactPhoneNumberArr objectAtIndex:indexPath.row];
     }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if(self.searchController.active){
        return [searchResultsArray count];
    }else{
    return  contactFullNameArr.count;
    }
}

//for fetching contacts
-(void)fetchContactsandAuthorization
{
    // Request authorization to Contacts
    CNContactStore *store = [[CNContactStore alloc] init];
    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted == YES)
        {
            //keys with fetching properties
            NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
            NSString *containerId = store.defaultContainerIdentifier;
            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
            NSError *error;
            NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];

            NSLog(@"new %@",cnContacts);

            if (error)
            {
                NSLog(@"error fetching contacts %@", error);
            }
            else
            {
                NSString *phone;
                NSString *fullName;
                NSString *firstName;
                NSString *lastName;
                UIImage *profileImage;
                NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init];
                for (CNContact *contact in cnContacts) {
                    // copy data to my custom Contacts class.
                    firstName = contact.givenName;
                    lastName = contact.familyName;

                    if (lastName == nil) {
                        fullName=[NSString stringWithFormat:@"%@",firstName];
                    }else if (firstName == nil){
                        fullName=[NSString stringWithFormat:@"%@",lastName];
                    }
                    else{
                        fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                    }
                    UIImage *image = [UIImage imageWithData:contact.imageData];
                    NSLog(@"imgold %@",image);
                    if (image != nil) {
                        profileImage = image;
                    }else{
                        profileImage = [UIImage imageNamed:@"person-icon.png"];
                    }
                    for (CNLabeledValue *label in contact.phoneNumbers) {
                        phone = [label.value stringValue];
                        if ([phone length] > 0) {
                            [contactNumbersArray addObject:phone];
                        }
                    }


                    [contactFullNameArr addObject:fullName];
                    [contactPhoneNumberArr addObject:phone];
                    [contactImgPathArr addObject:profileImage];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_selectContactListTblView reloadData];
                });
            }
        }
    }];
}

由于cellForRowAtIndexPath在我搜索时没有得到正确处理我得到了正确的名称,但我得到了其他细节错误。供参考

enter image description here

现在,如果我搜索第二个是kate的联系人。我正在将John缩略图和phonenumber映射到kate。任何帮助都将非常感激。

enter image description here

2 个答案:

答案 0 :(得分:0)

  1. 创建名为NSObject
  2. User个子类
  3. User.h添加3个属性 - fullName,phoneNumber,imagePath
  4. 在每个联系人的SelectContactViewController创建User对象中,并使用值填充属性。
  5. 将创建的User个对象添加到数组NSArray *contacts
  6. 创建第二个数组NSArray *filteredContacts并将其用作UITableView dataSource。
  7. updateSearchResultsForSearchController:更新为以下内容:

    -(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
        NSString *searchString = self.searchController.searchBar.text;
        NSPredicate *resultPredicate;
            resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchString];
        self.filteredContacts = [self.contacts filteredArrayUsingPredicate:resultPredicate];
        [_selectContactListTblView reloadData];
    }
    

答案 1 :(得分:0)

我的一个项目与此类似。请查看此信息以供参考。

class StoreVariables: NSObject {
var StoreList : NSMutableArray = NSMutableArray()
var StoreMob : NSMutableArray = NSMutableArray()
}

首先我创建了一个类" StoreVariables"。

var dataArray = StoreVariables()
var filteredArray = StoreVariables()
var shouldShowSearchResults = false

然后" dataArray"正常进入和" filteredArray"用于保持过滤列表。 " shouldShowSearchResults"用于检查searchBar的使用。

override func viewDidLoad() {
     storeclass = StoreVariables() // initialize
      searchbar.delegate = self
  }

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if shouldShowSearchResults {
        return filteredArray.StoreList.count
    }
    else {
        return storeclass.StoreList.count
    }

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell1") as! storeCell

    cell.backgroundColor = UIColor.clearColor()
    cell.selectionStyle  = UITableViewCellSelectionStyle.None
    if shouldShowSearchResults {
        cell.storeName.text! = filteredArray.StoreList[indexPath.row] as! String
        cell.storeMobile.text! = filteredArray.StoreMob[indexPath.row] as! String
    }
    else
    {
        cell.storeName.text! = storeclass.StoreList[indexPath.row] as! String
        cell.storeMobile.text! = storeclass.StoreMob[indexPath.row] as! String
        cell.delegate = self
    }
  }

// SearchBar委派

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    shouldShowSearchResults = false
    searchBar.text = ""
    storeTableView.reloadData()
    searchBar.resignFirstResponder()
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    if !shouldShowSearchResults {
        shouldShowSearchResults = true
        updateSearchResultsForSearchController(searchBar)
        storeTableView.reloadData()
    }

}
func updateSearchResultsForSearchController(searchBar: UISearchBar) {
    var searchString = searchbar.text
    searchString = searchString!.uppercaseString
    filteredArray.StoreList = []
    filteredArray.StoreMob = []
    if(storeclass.StoreList.count > 0)
    {
    for i in 0...storeclass.StoreList.count - 1{
        var mainString = storeclass.StoreList[i]
        mainString = mainString.uppercaseString
        let chk : Bool = (mainString.rangeOfString(searchString!, options: NSStringCompareOptions.CaseInsensitiveSearch ).location) != NSNotFound
        if (chk)
        {
            filteredArray.StoreList.addObject(storeclass.StoreList[i])
            filteredArray.StoreMob.addObject(storeclass.StoreMob[i])

        }
       }
    }
}