不同的字典和表格视图 [英] Different dictionaries and tableviews
问题描述
我有一本保存地址详细信息的字典,有时它可以保存姓名,电子邮件,电话"其他时间,仅保存姓名,移动电话"
I have a dictionary that hold address details, sometimes it can hold, 'name, email, telephone' other times, just 'name, mobile'
name = "Someone";
email = "Someone@somewhere.com";
telephone = "01000 000000";
或
name = "Someone Else";
mobile = "07700 000000";
我想在详细的表格视图中显示这些内容,在我看来,这是有道理的(我知道下面的代码不起作用,但是我不知道显示我想要发生的事情的任何其他方式):
I want to display these in a detail table view, in my head this makes sense (I know the below code doesn't work, but I don't know any other way of displaying what I want to happen):
if(selectedData objectForKey=@"name" at indexPath.row){
cell.textLabel.text = @"Name";
cell.detailTextLabel.text = [selectedData objectForKey:@"name"];
} else if(selectedData objectForKey=@"email" at indexPath.row){
cell.textLabel.text = @"Email";
cell.detailTextLabel.text = [selectedData objectForKey:@"email"];
} else if(selectedData objectForKey=@"telephone" at indexPath.row){
cell.textLabel.text = @"Telephone";
cell.detailTextLabel.text = [selectedData objectForKey:@"telephone"];
} else if(selectedData objectForKey=@"mobile" at indexPath.row){
cell.textLabel.text = @"Mobile";
cell.detailTextLabel.text = [selectedData objectForKey:@"mobile"];
}
但是我似乎无法正确地对此代码进行编码,我什至可能吠叫了错误的树!任何有关此欢迎的帮助或指示.
But I can't seem to get this coded correctly, and I might even be barking up the wrong tree! Any help or pointers on this welcome.
推荐答案
我设法通过首先在viewdidload中添加所有键的数组来对此进行排序:
I have managed to sort this by first adding an array of all the keys within viewdidload:
arrayOfKeys = [[NSMutableArray alloc]init];
for (NSString *key in [selectedData allKeys]){
NSLog(@"Key:%@", key);
if(key != @"title" && key != @"name"){
[arrayOfKeys addObject:key];
}
}
然后在我使用的cellForRowAtIndexPath中:
Then within cellForRowAtIndexPath I used:
NSString *currentKey = [arrayOfKeys objectAtIndex:indexPath.section];
if(currentKey == @"email"){
cell.textLabel.text = @"Email";
cell.detailTextLabel.text = [selectedData objectForKey:@"email"];
} else if(currentKey== @"telephone"){
cell.textLabel.text = @"Telephone";
cell.detailTextLabel.text = [selectedData objectForKey:@"telephone"];
} else if(currentKey == @"mobile"){
cell.textLabel.text = @"Mobile";
cell.detailTextLabel.text = [selectedData objectForKey:@"mobile"];
} else if(currentKey == @"company"){
cell.textLabel.text = @"Company";
cell.detailTextLabel.text = [selectedData objectForKey:@"company"];
}
PS.不要忘记在头文件中设置"arrayOfKeys".
PS. don't forget to set 'arrayOfKeys' in your header file.
这篇关于不同的字典和表格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!