【问题标题】:Filtered data doesn't show in my UITableView Objective C iOS7过滤后的数据未显示在我的 UITableView Objective C iOS7 中
【发布时间】:2014-06-23 08:21:55
【问题描述】:

我正在尝试在我的 UITableView 中绑定“搜索栏和搜索显示控制器”。过滤后的数组具有正确的数据,但它不显示在 tableview 中。当我在搜索栏中输入内容时,它会显示空白表格视图。 在函数“cellForRowAtIndexPath”中,当它绑定正确的数据但仍未显示时。在同一个函数中,我还更改了代码
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell" forIndexPath:indexPath];

UITableViewCell *cell = [self.personTableView dequeueReusableCellWithIdentifier:@"PersonCell" forIndexPath:indexPath];

然后它会抛出以下异常: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Person length]: unrecognized selector sent to instance 0x1780ca170”

代码:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [self.filteredPersonArray count];
 } else {
    return [self.personArray count];
}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell" forIndexPath:indexPath];

Person *person;
if (tableView == self.searchDisplayController.searchResultsTableView) {
    person = (Person *)[self.filteredPersonArray objectAtIndex:indexPath.row];
} else {
     person = (Person *)[self.personArray objectAtIndex:indexPath.row];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
nameLabel.text = [NSString stringWithFormat:@"%@ %@", person.firstName, person.lastName];
// so on;
return cell;
}

-(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
[self.filteredPersonArray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *lastNamepredicate = [NSPredicate predicateWithFormat:@"SELF.lastName contains[c] %@",searchText];
//NSPredicate *firstNamePredicate = [NSPredicate predicateWithFormat:@"SELF.firstName contains[c] %@",scope];
//tempArray = [tempArray filteredArrayUsingPredicate:lastNamepredicate];
self.filteredPersonArray = [NSMutableArray arrayWithArray:[self.personArray  
filteredArrayUsingPredicate:lastNamepredicate]];
}

-(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;
}

【问题讨论】:

    标签: objective-c uitableview ios7 unrecognized-selector


    【解决方案1】:

    这个问题的解决方法:
    我在创建单元格时引用了不同的 tableview 对象,以防
    1.搜索后加载数据
    2. 在tableview中加载数据

    - (UITableViewCell *)tableView:(UITableView *)tableView     cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell;
        Person *person;
        if (tableView == self.searchDisplayController.searchResultsTableView) 
        {
            cell = [self.personTableView dequeueReusableCellWithIdentifier:@"PersonCell"];
            person = (Person *)[self.filteredPersonArray objectAtIndex:indexPath.row];
        } else {
            cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell"];
            person = (Person *)[self.personArray objectAtIndex:indexPath.row];
        }
        UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
        nameLabel.text = [NSString stringWithFormat:@"%@ %@", person.firstName,     person.lastName];
        // so on;
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-19
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      相关资源
      最近更新 更多