【发布时间】:2013-03-04 00:52:13
【问题描述】:
我一直在尝试从 plist 文件中读取数据。这就是它的结构
|term|detail(string)|
我的属性:
@property (nonatomic, strong) NSDictionary *terms;
@property (nonatomic, strong) NSArray *termKeys;//this is just a array to keep track
@property (nonatomic, strong) NSString *detail;
这是我访问 cellForRowAtIndexPath 中详细信息的方式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
NSString *currentTermsName = [termKeys objectAtIndex :[indexPath row]];
[[cell textLabel] setText:currentTermsName];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
detail = [terms objectForKey:[termKeys objectAtIndex:indexPath.row]];
NSLog(@" bombs %@",terms[@"Bomb Threats"]);
return cell;
}
在视图中我有
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *myfile = [[NSBundle mainBundle]
pathForResource:@"terms" ofType:@"plist"];
terms = [[NSDictionary alloc] initWithContentsOfFile:myfile];
termKeys = [terms allKeys];
}
它访问值,但它为每个对象存储相同的值 假设我在 plist 中有 5 条不同的记录,如果我打印详细信息,它会显示相同的记录 5 次。
Once detail is set then I pass it to detialView
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"detailsegue"]){
TermsDetailViewController *controller = (TermsDetailViewController *)segue.destinationViewController;
controller.detailTerm = detail;
}
}
这是我的字典: http://pastebin.com/bxAjJzHp
【问题讨论】:
-
你是什么意思“它为每个对象存储相同的”?
-
假设我在 plist 中有 5 条不同的记录,如果我打印
detail它会显示相同的记录 5 次 -
我猜你应该使用这个:
detail = [ [ terms objectForKey:@"termKeys objectAtIndex:indexPath.row ] ] objectForKey:@"detail" ]
标签: objective-c uitableview plist