В подклассе 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?
Что самый простой путь состоял в том, чтобы сделать это?
Я не понимаю, почему вы хотите использовать словарь (который по наследству неупорядочен) для задачи, требующей ответов на упорядоченные вопросы (строки), но я полагаю, что у вас уже есть словарь откуда-то и вы не можете это изменить. Если это так, вы должны определить порядок, в котором вы хотите отображать ключи, тем самым создавая массив неявно. Один из способов сделать это - упорядочить в алфавитном порядке еще один следующий:
// 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]];
value теперь является объектом, выбранным в случае tableView: didSelectRowAtIndexPath: или объектом, который вам нужен для обработки вашей ячейки в tableView: cellForRowAtIndexPath:
Если базовые изменения NSDictionary, вам необходимо перезагрузить ( [myTable reload]
и т.п.) UITableView.
Да. Вот как мы это сделали:
В нашем синтаксическом анализаторе 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];
Это, вероятно, больше, чем вы хотели, но это общий план.