表视图和谓词

时间:2013-10-15 05:24:59

标签: iphone ios uitableview

我有一个包含40个对象的表视图。点击UISegment(男性,女性和两者)时,我必须按性别过滤它们。它似乎工作,但表视图不刷新。请任何帮助将是欣赏的。

  -(void)viewDidLoad
{
  [super viewDidLoad];

 self.navigationItem.title = @"People";   
    [self loadall];

  [[NSBundle mainBundle] loadNibNamed:@"FilterSortView" owner:self options:nil];

  self.filterControl.selectedSegmentIndex = -1;
  [self.filterControl addTarget:self action:@selector( changeSegge ) forControlEvents:UIControlEventValueChanged];

}

我的第二种按性别过滤的方法

- (void)changeSegge
{
    NSEntityDescription *personEntity = [NSEntityDescription entityForName:@"Person"
                                                    inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:personEntity];
    NSError *error = nil;
    self.people = [self.managedObjectContext executeFetchRequest:request error:&error];
    if (error)
    {
        [NSException raise:NSInternalInconsistencyException format:@"Could not fetch Core Data records: %@",error];
    }

    if(filterControl.selectedSegmentIndex == 0){

        NSPredicate *predicatem =[NSPredicate predicateWithFormat:@"gender == %@", @"m" ];
        request.predicate=predicatem;

        [self.tableView reloadData];
        [request release];
        NSLog(@"button 1");    

   }

非常感谢。

1 个答案:

答案 0 :(得分:0)

在谓词设置为获取请求后,您忘记填充人员数组。

以下是更新的changeSegge方法:

- (void)changeSegge
{
    NSEntityDescription *personEntity = [NSEntityDescription entityForName:@"Person"
                                                    inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:personEntity];
    NSError *error = nil;
    self.people = [self.managedObjectContext executeFetchRequest:request error:&error];
    if (error)
    {
        [NSException raise:NSInternalInconsistencyException format:@"Could not fetch Core Data >records: %@",error];
    }

    if(filterControl.selectedSegmentIndex == 0){

        NSPredicate *predicatem =[NSPredicate predicateWithFormat:@"gender == %@", @"m" ];
        request.predicate=predicatem;
        self.people = [self.managedObjectContext executeFetchRequest:request error:&error];
        [self.tableView reloadData];
        [request release];
        NSLog(@"button 1");    

    }