2012-08-11 90 views
0

如果我想显示所有对象,我使用下面的代码。但是,如果我只想显示键“收藏夹”=“是”的值的对象,那么是什么?这样的例子会很好。在UITableView中显示具有特定字符串值的对象

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

Ps。属性列表是一个包含字典的数组。

+0

我不认为你已经发布的所有代码,以显示所有的元素 - 不要你需要一个'cellForRowAtIndexPath'功能,在你的表实际上使用来自'sortedObjects'的数据? – 2012-08-11 01:00:48

+0

@ajd呃......单元格是在cellForRowAtIndexPath中创建和填充的。但是,如果有的话,我用来填充单元格的对象在viewDidLoad中定义。 numberOfRowsInSection正在对对象进行计数,所以我认为这是所有需要回答的问题的代码。 – ingenspor 2012-08-11 01:08:46

回答

1

您需要创建一个新数组,让它过滤,通过过滤sortedObjects创建,然后使用它来填充表。

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF.Favorite == %@", @"Yes"]; 
NSArray *filtered = [sortedObjects filteredArrayUsingPredicate:pred]; 
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 
    } 
} 
相关问题