[英]How to read plist file then populate UItableView?
I have been trying read data from a plist file. 我一直在尝试从plist文件读取数据。 this is how it is structured
这就是它的结构
|term|detail(string)|
my properties : 我的财产:
@property (nonatomic, strong) NSArray *terms;
@property (nonatomic, strong) NSArray *termKeys;//this is just a array to keep track
@property (nonatomic, strong) NSString *detail;
this is how I access the detail in the cellForRowAtIndexPath
这是我访问
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 objectAtIndex:indexPath.row];
NSLog(@" bombs %@",terms[@"Bomb Threats"]);
return cell;
}
and in view didload I have 鉴于didload我有
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *myfile = [[NSBundle mainBundle]
pathForResource:@"terms" ofType:@"plist"];
terms = [[NSDictionary alloc] initWithContentsOfFile:myfile];
termKeys = [terms allKeys];
}
It accesses the values , but it store the same one for each object lets say I have 5 different records in plist, if i print detail it displays the same record 5 times. 它访问值,但为每个对象存储相同的值,可以说我在plist中有5条不同的记录,如果我打印详细信息,它将显示5条相同的记录。
Once detail is set then I pass it to detialView 设置好细节之后,我会将其传递给detialView
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"detailsegue"]){
TermsDetailViewController *controller = (TermsDetailViewController *)segue.destinationViewController;
controller.detailTerm = detail;
}
}
and finaly : 最后:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"detailsegue" sender:self];
}
Here is my dictionary : http://pastebin.com/bxAjJzHp 这是我的字典: http : //pastebin.com/bxAjJzHp
The goal is to pass the detail information to a detailviewcontroller just like a Master/detail sample project. 目的是将详细信息传递给detailviewcontroller,就像主/详细示例项目一样。
You cannot set the detail
variable in the cellForRowAtIndexPath:
method, because this way the value becomes transient: it depends on how the user scrolls through the table, not on what disclosure button the user clicks. 您无法在
cellForRowAtIndexPath:
方法中设置detail
变量,因为这种方式该值变为瞬态的:它取决于用户如何滚动浏览表格,而不取决于用户单击的显示按钮。
Move this line 移动这条线
detail = [terms objectAtIndex:indexPath.row];
to the didSelectRowAtIndexPath:
before the call of performSegueWithIdentifier:
to fix the problem. 到
didSelectRowAtIndexPath:
的调用之前performSegueWithIdentifier:
来解决这个问题。 Now the detail
is set in response to user's click immediately before the prepareForSegue:
call, making sure that the correct value is passed to the view controller with the details data. 现在,将根据用户在
prepareForSegue:
调用之前的单击来设置detail
,以确保将正确的值与详细信息数据一起传递给视图控制器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.