Можно ли использовать NSDictionary с TableView на iPhone? - PullRequest
6 голосов
/ 17 апреля 2010

В подклассе UITableViewController есть несколько методов, которые необходимо реализовать для загрузки данных и обработки события выбора строки:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1; //there is only one section needed for my table view
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {              
    return [myList count]; //myList is a NSDictionary already populated in viewDidLoad method
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease ];
    }

    // indexPath.row returns an integer index, 
    // but myList uses keys that are not integer, 
    // I don't know how I can retrieve the value and assign it to the cell.textLabel.text


    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Handle row on select event, 
    // but indexPath.row only returns the index, 
    // not a key of the myList NSDictionary, 
    // this prevents me from knowing which row is selected


}

Как NSDictionary должен работать с TableView?

Какой самый простой способ сделать это?

Ответы [ 2 ]

23 голосов
/ 17 апреля 2010

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

// a) get an array of all the keys in your dictionary
NSArray* allKeys = [myList allKeys];
// b) optionally sort them with a sort descrriptor (not shown)
// c) get to the value at the row index
id value = [myList objectForKey:[allKeys objectAtIndex:indexPath.row]];

значение теперь является объектом, выбранным в случае tableView: didSelectRowAtIndexPath: или объектом, который необходим для обработки вашей ячейки в tableView: cellForRowAtIndexPath:

Если базовый NSDictionary изменяется, вам необходимо перезагрузить ([myTable reload] или подобное) UITableView.

3 голосов
/ 17 апреля 2010

Да. Вот как мы это сделали:

В нашем анализаторе xml есть метод, который загружает xml в словарь dict:

-(NSDictionary *)getNodeDictionary:(Node *)node {
    if (node->level == 0) return xmlData;
    else {
        NSDictionary *dict = xmlData;
        for(int i=0;i<node->level;i++) {
            if ([[dict allKeys] containsObject:SUBNODE_KEY])
                dict = [[dict objectForKey:SUBNODE_KEY]   objectAtIndex:*(node->branches+i)];
        }
        return dict;
    }
}

И этот метод

-(NSDictionary *)getDataForNode:(Node *)node {
NSDictionary* dict = [[self getNodeDictionary:node] copy];
return dict;

}

В классе RadioData у нас есть переменная экземпляра:

Node *rootNode;

и куча методов

-(Node *)getSubNodesForNode:(Node *)node;
-(Node *)getSubNodeForNode:(Node *)node atBranch:(NSInteger)branch;
-(Node *)getParentNodeForNode:(Node *)node;
-(NSInteger)getSubNodeCountForNode:(Node *)node;
-(NSDictionary *)getDataForNode:(Node *)node;

и собственность

@property (nonatomic) Node *rootNode;

Наконец, в ViewController, когда мы запускаем фрейм, который мы используем:

radioData = data;
curNode = data.rootNode;

и внутри cellForRowAtIndexPath мы имеем:

Node* sub = [radioData getSubNodeForNode:curNode atBranch:indexPath.row];
NSDictionary* dets = [radioData getDataForNode:sub];    

и в didSelectRowAtIndexPath:

    Node* node = [radioData getSubNodeForNode:curNode atBranch:indexPath.row];
NSDictionary* data = [radioData getDataForNode:node];

Это, вероятно, больше, чем вы хотели, но это общий план.

...