как отобразить массив словарей в виде таблицы в iphone - PullRequest
0 голосов
/ 17 февраля 2012

Привет, друг, я хочу, чтобы имя и фамилия отображались в ячейке, но отображается только имя. Я пытаюсь ответить на какое-то заданное мной эту ссылку

Этот код используется для получения 1-й буквыкаждого штата США (stateName) для группировки их в алфавитном порядке:

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 для сортировки массива

- (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 голосов
/ 17 февраля 2012

Примерно так: в конце кода:

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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...