2013-02-01 33 views
2

我已經創建了一個數組,兩個字典和一個tableview。 tableview應顯示數組的內容,該數組包含字典的數據。當我嘗試顯示它使用「[mArray addObject:(mDictionary)];」它拋出一個異常,但如果我使用「[mArray addObject:[mDictionary objectForKey:@」firstname「]];」它的工作正常。在表格視圖中顯示數組和字典值

你能幫我理解爲什麼會發生這種情況嗎?如果不使用「objectForKey」就不能顯示字典值?

這是我的「ViewController.m」。

#import "ViewController.h" 

@implementation ViewController 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    mArray = [[NSMutableArray alloc]init]; 
    mDictionary = [[NSMutableDictionary alloc]init]; 
    [mDictionary setValue:@"shanu" forKey:@"firstname"]; 
    [mDictionary setValue:@"kumar" forKey:@"lastname"]; 
    mDictionary2 = [[NSMutableDictionary alloc]init]; 
    [mDictionary2 setValue:@"hgjghjgf" forKey:@"firstname1"]; 
    [mDictionary2 setValue:@"ghjgjgfj" forKey:@"lastname1"]; 
    [mArray addObject:mDictionary]; 
    [mArray addObject:mDictionary2]; 

    CGRect frame = self.view.frame; 
    UITableView *tableView = [[UITableView alloc]initWithFrame:frame style:UITableViewStylePlain]; 
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
    tableView.backgroundColor = [UIColor cyanColor]; 
    tableView.separatorColor = [UIColor blackColor]; 
    tableView.delegate = self; 
    tableView.dataSource = self; 
    [self.view addSubview:tableView]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return @"Array and Tableview"; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    return mArray.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 
    cell.textLabel.text = [mArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

- (void)dealloc 
{ 
    [mArray release]; 
    [mDictionary release]; 
    [mDictionary2 release]; 
    [super dealloc]; 
} 
@end 

回答

2

如果嘗試分配一些文本的單元格的爲textLabel從返回的對象:

[mArray objectAtIndex:indexPath.row]; 

應該是一個字符串,而不是一個整體的字典。

當你只是把整個字典放在數組[mArray objectAtIndex:indexPath.row];將返回一個字典,你不能指定一個字典的文本標籤,所以你會得到一個異常。如果你想存儲整個字典使用objectForKey來獲取字符串你想要的:

cell.textLabel.text = [[mArray objectAtIndex:indexPath.row] objectForKey:@"WHAT_EVER_KEY_YOU_WANT"]; 

順便說一句,你不使用ARC ZO你可能想獲得一個自動釋放的UITableViewCell這麼加autorelease到你的細胞:

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease]; 

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
[cell autorelease]; 
+0

這意味着如果沒有使用鍵值我們不能顯示字典數據? – Newbee

+0

是的,並使用'objectForKey'來獲取正確的NSString對象。那麼......你也可以使用字典的描述爲textlabel如果這是你想要的:'[[mArray objectAtIndex:indexPath.row] description];' – Tieme

0

數組包含類似的類型對象。您可以將字典對象添加到數組中。在這種情況下沒有問題。問題在於何時從數組中獲取元素並將其分配給cell.textLabel.text。這裏的對象是字典的類型,cell.textLabel.text是nsstring的類型。所以你應該爲字典對象的鍵值賦給cell.textLabel.text。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 
    cell.textLabel.text = [[mArray objectAtIndex:indexPath.row] objectForKey:@"firstname"]; 
cell.detialTextLabel.text = [[mArray objectAtIndex:indexPath.row] objectForKey:@"lastname"]; 
     return cell; 
    } 

我認爲這會對您有所幫助。

+0

我使用上面的代碼,但它只顯示名字值。 – Newbee

+0

將UITableViewCellStyleDefault更改爲UITableViewCellStyleSubtitle,並且所有字典對象中的更改鍵名也應相同。 –

+0

我改變根據你,但現在它拋出一個例外.. – Newbee

5

中聲明.h文件中的數組:

NSArray *keys 

寫這個在viewDidLoad中

keys = [mDictionary allKeys]; 

這的cellForRowAtIndexPath

cell.textLabel.text = [mDictionary objectForKey:[keys objectAtIndex:indexPath.row]; 
+0

嘗試,但它不工作.. – Newbee

+0

你在這個面臨什麼問題? – CRDave

0

這解決了我的問題

NSDictionary *cellDict = [self.tableInfoArr objectAtIndex:indexPath.row]; 

//This is to don't care about what is key and value in that dictionary 
NSArray *keyArr = [cellDict allKeys]; 

//As at each index of array only one dictionary exist. 
NSString *keyStr = [keyArr objectAtIndex:0]; 

cell.titleLabel.text = keyStr; 
cell.detailLabel.text = [cellDict valueForKey:keyStr];