0

我正在使用Core DataUITableViewController。我有一个category带有列namedescriptis_active布尔值)的表。

我想获取类似的结果

SELECT * 
FROM CATEGORY 
WHERE IS_ACTIVE = 1

我已经通过一个表格来激活它。

我正在尝试这段代码 - 鉴于我有这个加载部分

- (void)viewDidLoad
{
    [super viewDidLoad];
    IMSAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
    context = [appDelegate managedObjectContext];
    NSEntityDescription *category = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:context];
      NSFetchRequest *request = [[NSFetchRequest alloc] init];


    [request setEntity:category];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *srotDesc = [[NSArray alloc]initWithObjects:sort, nil];

    [request setSortDescriptors:srotDesc];

    NSError *error;
     NSMutableArray *results = [[context executeFetchRequest:request error:&error] mutableCopy];
    if (results == nil) {
        //error handle here
    }

    [self setArr:results];
    NSLog(@"there is category array");

    [self.tableView reloadData];
    [arr count];

    }

并在 tableViewCell

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

    Category *category = [arr objectAtIndex:indexPath.row];

    // Configure the cell...
    cell.textLabel.text = [category name];
    cell.detailTextLabel.text = [category descript];

    return cell;
}

它向我展示了所有的记录,就像所有的名称和描述一样。

我只想在 is_active = 1 时显示这些记录。

4

1 回答 1

0

将谓词添加到您的 NSFetchRequest

...
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:category];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"is_active == 1"];
[request setPredicate:predicate];
...
于 2013-09-25T18:50:43.797 回答