2012-08-11 2 views
0

모든 개체를 표시하려면 아래 코드를 사용하십시오. 그러나 키의 값이 "Favorite"= "Yes"인 객체 만 표시하려면 무엇을해야합니까? 이것의 예는 좋을 것입니다.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

난 당신이 모든 요소를 ​​표시하는 모든 코드를 게시 한 생각하지 않아? –

+0

@ajd 음 .. 세포가 만들어지고 cellForRowAtIndexPath에 채워집니다. 그러나 객체가 있으면 셀을 채우는 데 사용하여 viewDidLoad에 정의됩니다. numberOfRowsInSection은 객체를 계산하므로이 질문에 대한 답변이 필요한 모든 코드라고 생각하십니까? – ingenspor

답변

1

새 배열을 만들거나, 필터링하여 호출하거나, sortedObjects를 필터링하여 만든 다음이를 사용하여 테이블을 채워야합니다.

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF.Favorite == %@", @"Yes"]; 
NSArray *filtered = [sortedObjects filteredArrayUsingPredicate:pred]; 
0

다음 코드를 추가하십시오. 당신이 실제로 테이블에`sortedObjects`의 데이터를 사용하기 위해`cellForRowAtIndexPath` 기능이 필요하지 않습니다 -

- (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 
    } 
} 
관련 문제