【发布时间】:2014-02-21 09:39:01
【问题描述】:
我是 iPhone 新手,根据要求,我必须从 tableview 的问题列表中选择一个问题,并希望在下一个新视图中显示该问题。所以我将我的行值发送到新的 Xib 文件创造的时间。
我将问题答案存储在 plist 中作为字典格式的数组。我可以从 Plist 中获取数组,但无法获取字典数据。我创建了自定义类来存储问题答案。但是在新的 get只有存储在 plist 中的最后一个问题。 请帮助我。谢谢你..
在这里我附上我的代码tableView.m,newView.m 和customClass.m..
tableView.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@" index row value:%d",indexPath.row);
NSInteger row=indexPath.row;
questionViewController *viewController1 =
[[questionViewController alloc] initWithNibName:@"questionViewController" bundle:[NSBundle mainBundle]];
viewController1.rowValue=row;
self.viewController = viewController1;
[self.viewController.navigationItem setTitle:@"Ques Details"];
[self.navigationController pushViewController:self.viewController animated:YES];
}
在新的 Xib 文件中,在 ViewDidLoadFunction 中
newView.m
- (void)viewDidLoad
{
//rowValue tells me which row is selected
[self firstView:rowValue];
}
-(void)firstView: (NSInteger)indexNumber{
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"Qus_ans" ofType:@"plist"];
NSArray *rawElementsArray = [[NSArray alloc] initWithContentsOfFile:thePath];
NSDictionary *eachElement;
//overlap the data here
for (eachElement in rawElementsArray)
{
element = [[Question alloc] initWithDictionary:eachElement];}
//get only last question in the plist
questionLabel.text =[NSString stringWithFormat:@"%@",element.ques];
}
element is my Custom class object.This is my custom class:
Question.m
- (id)initWithDictionary:(NSDictionary *)aDictionary {
self = [[Question alloc] init];
if (self) {
self.ques = [aDictionary valueForKey:@"question"];
self.ans = [aDictionary valueForKey:@"answer"];
[self data];
}
return self;
}
Here element.ques is overlapped with the last value.
How I get proper question without using the forLoop...
这是我的 plist 格式
<array>
<dict>
<key>question</key>
<string>Approximately how much does the human brain weigh?</string>
<key>answer</key>
<string>Three pounds</string>
</dict>
<dict>
<key>question</key>
<string>The brain has this number of The brain has this number of nerve cells?</string>
<key>answer</key>
<string>1 to 100 billion</string>
</dict>
【问题讨论】:
-
请格式化您的问题。很难阅读。
-
这就是为什么我之前提到我是 iPhone 新手,但我想我清楚地提到了每个功能的用途。
-
此链接帮助。在发布搜索 stackoverflow 天气之前,这个问题是否可用
-
谢谢先生。但我想要类似的东西 - 我正在从 didSelectRowAtIndexPath 中选择特定数据,并希望从 plist.through 的字典数组中获取特定行号字典的键值循环我可以获取 plist 的全部数据,但是我如何从字典中存储特定的行键。