0

すべてのオブジェクトを表示したい場合は、以下のコードを使用します。しかし、キー「Favorite」の値が「Yes」であるオブジェクトのみを表示したい場合は、どうすればよいでしょうか。この例は素晴らしいでしょう。

- (void)viewDidLoad
{
[super viewDidLoad];    

NSString *path = [[NSBundle mainBundle]
                    pathForResource:@"Objects" ofType:@"plist"];

sortedObjects = [[NSMutableArray alloc]initWithContentsOfFile:path];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [sortedObjects count];
}

追伸 プロパティリストは、辞書を含む配列です。

4

2 に答える 2

1

新しい配列を作成し、それをfilteredと呼び、sortedObjectsをフィルタリングして作成し、それを使用してテーブルにデータを入力する必要があります。

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF.Favorite == %@", @"Yes"];
NSArray *filtered = [sortedObjects filteredArrayUsingPredicate:pred];
于 2012-08-11T01:11:39.360 に答える
0

次のコードを追加してみてください。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"cellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{
    if([[sortedObjects objectAtIndex:indexPath.row] valueForKey:@"Favorite"] isEqualToString:@"YES"])
    {
        //do stuff
    }
}
于 2012-08-11T01:15:06.427 に答える