2012-08-11 83 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 
    } 
} 
相關問題