我有一本字典,其中包含地址详细信息,有时可以保留,“姓名,电子邮件,电话”,其他时间,只是'姓名,移动'
name = "Someone";
email = "Someone@somewhere.com";
telephone = "01000 000000";
或
name = "Someone Else";
mobile = "07700 000000";
我想在详细的表视图中显示这些,在我的脑海中这是有意义的(我知道下面的代码不起作用,但我不知道任何其他方式显示我想要发生的事情):< / p>
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"];
}
但我似乎无法正确编码,我甚至可能会咆哮错误的树! 任何有关此欢迎的帮助或指示。
答案 0 :(得分:0)
我设法通过首先在viewdidload中添加所有键的数组来对其进行排序:
arrayOfKeys = [[NSMutableArray alloc]init];
for (NSString *key in [selectedData allKeys]){
NSLog(@"Key:%@", key);
if(key != @"title" && key != @"name"){
[arrayOfKeys addObject:key];
}
}
然后在cellForRowAtIndexPath中使用:
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'。