Ios 如何将整个索引路径、previous、before previous和current selected index path值存储到didSelectRowAtIndexPath中的NSArray:方法
问题: 我开发测验应用程序。在应用程序中,一个表视图中有5个问题。问题来自plist文件。每当我在tableview中选择问题选项时,它都会显示当前选定的索引路径值 但是我想要所有的问题选项,比如数组中1到5个选定的索引路径 我的代码:Ios 如何将整个索引路径、previous、before previous和current selected index path值存储到didSelectRowAtIndexPath中的NSArray:方法,ios,objective-c,iphone,uitableview,ios7,Ios,Objective C,Iphone,Uitableview,Ios7,问题: 我开发测验应用程序。在应用程序中,一个表视图中有5个问题。问题来自plist文件。每当我在tableview中选择问题选项时,它都会显示当前选定的索引路径值 但是我想要所有的问题选项,比如数组中1到5个选定的索引路径 我的代码: (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // what is that code,....? } 我想:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// what is that code,....?
}
我想:
NSLog(@"whole index_path : %@",array name);
输出:
整个索引路径:
0
1.
0
2.
一,
嗯。你必须做三件事:
-(void)prepareforsgue:(UIStoryboardSegue*)分段发送方:(id)发送方{
如果([segue.identifier IsequalString:@“AnalysisScreen”]){
AnalysisViewController*AnalysisViewController=segue.destinationViewController;
analysisViewController.CurrentQuike=self.CurrentQuike;
}
}
Quizcell.TotalQuestions.backgroundColor=[UIColor-whiteColor]代码>
使用以下行:
Question*Question=self.currentquick[indexPath.row];
Quizcell.TotalQuestions.backgroundColor=(question.selectedResponse==question.correctResponse)?[UIColor greenColor]:[UIColor redColor]代码>
希望我没弄错!:) 创建一个NSMutableArray
并将索引路径添加为对象。无论何时显示一个问题,比如它是否是第一个问题,都可以从数组中访问它的对象,查看选择了哪个索引路径,并将该行设置为选中,对于其他问题也是如此
但我建议您不要在数组中保留索引路径,而是创建一个具有问题id的NSObject
类,选择的答案id
作为成员,当您显示一个问题时,该问题的用户选择时间早于访问选择的答案id
,并将其显示为选中状态。假设您有5个问题,每个问题最多可以有5个选项
您有一个带有表格视图的屏幕,您一次在表格视图中显示一个带有选项(2-5行)的问题,您将有一些选项供用户导航到下一个问题(在这种情况下,您将使用下一个问题重新加载表格)
这是你的设置
现在,根据我的建议,创建一个NSObject类,为每个问题保留所选选项的信息
@interface CustomObject : NSObject
{
NSInteger _questionId;
NSInteger _answerId;
}
@property (nonatomic,readwrite,assign) NSInteger questionId;
@property (nonatomic,readwrite,assign) NSInteger answerId;
@end
#import "CustomObject.h”
@implementation CustomObject
@synthesize questionId = _questionId, answerId = _answerId;
-(id) initWithQuestionId:(NSUInteger)qId answerId:(NSUInteger)aId
{
if( (self=[super init]) ) {
_questionId = qId;
_answerId = aId;
}
return self;
}
@end
现在假设对于这5个问题,您的id
为1,2,3,4,5
,并且最初没有选择答案,因此为选择的答案id
保留一个不适用的id作为-1
在类中创建一个NSMutableArray
,将表视图显示为
NSMutableArray *m_selectedAnsInfoList;
根据需要初始化上述数组一次
m_selectedAnsInfoList = [NSMuatbleArray alloc] init];
最初为每个问题创建CustomObject类的对象,如下所示
(假设我们有5个问题
,所以questionCount=5
)
通过这种方式,您可以存储为每个问题选择的答案的信息,并根据列表中相应对象中存储的信息显示所选答案。所有问题都在一个表视图中?或者它们是一个接一个地显示的?单表视图,但我使用它本身的segue方法,从plist获取问题如果选择一个测验选项,它将在同一个表视图中导航下一个问题。我尝试过,但我得到了2015-05-06 18:23:39.619IQ[38228:2439606]Total_Index:(“{length=2,path=1-1}”)它显示最后一个选定的索引路径,但我想要整个选定的索引路径,我没有收到错误信息,它只显示上次选择的索引路径值。是否要共享您的项目,以便我可以更好地了解出了什么问题?好的,我尝试了您的项目。现在到底是什么问题?:)在应用程序中有3个问题。我想要结果结束视图,一个选项是有分析。如果你点击它,就会显示有多少问题是这样的。我希望集合视图单元格显示有多少问题可能是对的和错的,红色(错误)/绿色(正确)你能在这个问题中给出一些例子吗asked@Tamil_Arya我发布了另一个答案,你可以用同样的方法来实现。我试过了,但我得到了错误,只是因为你的方法对我来说很难。谢谢。我会发布我的项目,你能帮我吗?非常感谢你的回复兄弟。
for (int i=1; i <= questionCount : i++)// will iterate for 5 times
{
//create instance of CustomObject with current iteration count as question id and
// initially -1 for answer id, as for the first time no answer will be selected
CustomObject *obj = [[CustomObject alloc] initWithQuestionId:i answerId:-1];
// add object to the array
[m_selectedAnsInfoList addObject:obj];
[obj release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// currentQuestionId keeps id of the table view showing current question
// using which access the questionInfoObject
obj = [m_selectedAnsInfoList objectAtIndex:currentQuestionId];
// set answer id in object
obj.answerId = indexPath.row; (from 0 to (number of answers - 1));
// replace old object with updated obj in array as
[m_selectedAnsInfoList replaceObject:obj atIndex:currentQuestionId];
}