2010-04-17 11 views
6

Trong một lớp con UITableViewController, có một số phương pháp mà cần phải được thực hiện để tải dữ liệu và xử lý các sự kiện lựa chọn hàng:NSDictionary có thể được sử dụng với TableView trên iPhone không?

- (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 


} 

như thế nào NSDictionary vụ phải làm việc với TableView?

Cách đơn giản nhất để thực hiện việc này là gì?

Trả lời

23

Tôi không hiểu tại sao bạn muốn sử dụng từ điển (không thừa kế) cho tác vụ yêu cầu câu trả lời cho các câu hỏi (hàng), nhưng tôi lấy nó là bạn đã có từ điển ở đâu đó và không thể thay đổi . Nếu trường hợp đó xảy ra, bạn phải xác định thứ tự bạn muốn hiển thị các khóa, do đó phát sinh một mảng ngầm. Một cách để làm điều này là theo thứ tự abc đặt hàng một số khác là những điều sau đây:

// 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]]; 

giá trị bây giờ là đối tượng được chọn trong trường hợp của tableView: didSelectRowAtIndexPath: hoặc đối tượng bạn cần cho việc xử lý di động của bạn trong tableView: cellForRowAtIndexPath:

Nếu NSDictionary bên dưới thay đổi, bạn phải tải lại ([myTable reload] hoặc tương tự) UITableView.

+0

Giải pháp của bạn đủ đơn giản đối với tôi. – bobo

+0

Giải pháp đơn giản nhưng điều này đã giúp tôi rất nhiều! – stitz

3

Có. Dưới đây là làm thế nào chúng tôi đã thực hiện nó:

Trong phân tích cú pháp xml, chúng ta có phương pháp này mà tải xml vào một từ điển được gọi là 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; 
    } 
} 

Và phương pháp này

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

}

Trong lớp RadioData, chúng tôi có một biến mẫu:

Node *rootNode; 

và một loạt các phương pháp

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

và một tài sản

@property (nonatomic) Node *rootNode; 

Cuối cùng trong ViewController khi chúng ta init khung chúng tôi sử dụng:

radioData = data; 
curNode = data.rootNode; 

và bên cellForRowAtIndexPath ta có:

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

và trong didSelectRowAtIndexPath:

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

Đây có lẽ là nhiều hơn bạn muốn nhưng đó là phác thảo chung.

+0

Cảm ơn rất nhiều vì giải pháp của bạn. Nhưng đó là một điều phức tạp đối với tôi. – bobo

+0

Có ... một chút phức tạp của nó nhưng ví dụ là từ một ứng dụng khá phức tạp. Thật không may, tôi không có một ví dụ đơn giản hơn trong tầm tay. Điều này nên, tuy nhiên, cung cấp cho bạn một điểm khởi đầu. Có lẽ việc sử dụng mảng có thể dễ dàng hơn. –