如何在iphone的tableview中显示字典数组

时间:2012-02-17 07:01:56

标签: iphone objective-c

嗨朋友,我希望在单元格上显示名字和姓氏,但只显示名字我试着回答给我一些this link

这是用于获取每个美国州(州名称)的第一个字母的代码,以便按字母顺序将它们组合在一起:

personarray = [[NSMutableArray alloc]init];
    [personarray addObjectsFromArray:dislist.Peoplelistarray];
    //short the personarray value:
    NSSortDescriptor *asortDescriptor;
    asortDescriptor=[NSSortDescriptor sortDescriptorWithKey:@"FirstName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    //asortDescriptor=[[NSSortDescriptor alloc]initWithKey:@"TagName" ascending:YES];
    NSArray *sortDescriptors=[NSArray arrayWithObject:asortDescriptor];
    [self.personarray sortUsingDescriptors:sortDescriptors];

    //---create the index---
    Frstname = [[NSMutableArray alloc] init];
    //check
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    for (NSDictionary *row in personarray) {
        [tempArray addObject:[row valueForKey:@"FirstName"]];

        for (int i=0; i<[tempArray count]; i++){
            char alphabet = [[tempArray objectAtIndex:i] characterAtIndex:0];
            NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet];
            if (![Frstname containsObject:uniChar]){  
                [Frstname addObject:uniChar];



            }

我在使用@“FirstName"对数组进行排序后,了解如何使用@“LastName"NSPredicate(作为副标题)填充tableview单元格时遇到问题

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [Mytableview dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    if (isSearchOn) {
        NSString *cellValue = [searchResult objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
    }
    else {

    //---get the letter in the current section---
    NSString* alphabet = [Frstname objectAtIndex:[indexPath section]];
    //---get all states beginning with the letter---
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
    NSArray *Names = [[personarray valueForKey:@"FirstName"] filteredArrayUsingPredicate:predicate];
    if ([Names count]>0) {
        //---extract the relevant firstname from the Names object---
        NSString *cellValue = [Names objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;

    }   

1 个答案:

答案 0 :(得分:0)

在代码的最后,这样的事情怎么样:

NSArray *firstNames = [[personarray valueForKey:@"FirstName"] filteredArrayUsingPredicate:predicate];
NSArray *lastNames = [[personarray valueForKey:@"LastName"] filteredArrayUsingPredicate:predicate];
    if ([firstNames count]>0) {
        //---extract the relevant firstname from the Names object---
        NSString *firstName = [firstNames objectAtIndex:indexPath.row];
NSString *lastName = [lastNames objectAtIndex:indexPath.row];
NSString *cellValue = [NSString stringWithFormat:@"%@ %@",firstName, lastName];
        cell.textLabel.text = cellValue;
}