0

と を使用Core DataしてUITableViewControllerいます。、および(ブール値)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 に答える