如何在 didSelectRowAtIndexPath: 方法中将整个索引路径、前一个、前一个和当前选择的索引路径值存储到 NSArray

How to store Whole index path, previous,before previous and current selected index path value to the NSArray within didSelectRowAtIndexPath: methods

问题:

我开发测验应用程序。在应用程序中,一个表格视图中有 5 个问题。问题取自 plist 文件。每当我在表格视图中选择问题选项时,它都会显示当前选择的 index_path 值。

但我想在数组中选择 index_path 中的所有问题选项,例如从 1 到 5。

我的代码:

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    // what is that code,....?

}

我要:

NSLog(@"whole index_path : %@",array name);

输出:

whole index_path : 0 1 0 2 1

好的。你必须做三件事:

  1. 转到您的故事板,并为从结果视图控制器到分析视图控制器的转接提供一个标识符 "AnalysisScreen"

  2. 转到您的 resultsviewcontroller.m 并实施以下方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"AnalysisScreen"]) { AnalysisViewController *analysisViewController = segue.destinationViewController; analysisViewController.currentQuiz = self.currentQuiz; } }

  1. 转到您的 analysisviewcontroller.m 并在 cellForItemAtIndexPath 中替换行

Quizcell.TotalQuestions.backgroundColor = [UIColor whiteColor];

包含以下几行:

Question *question = self.currentQuiz[indexPath.row]; Quizcell.TotalQuestions.backgroundColor = (question.selectedResponse == question.correctResponse) ? [UIColor greenColor] : [UIColor redColor];

希望我没猜错! :)

创建一个 NSMutableArray 并添加 index paths 作为对象。每当你展示一个问题,比如它是否是第一个问题时,从数组访问它的对象并查看选择了哪个索引路径并将该行设置为选中,其他问题也类似。

但我建议您不要将索引路径保留在数组中,而是创建一个 NSObject class 并以 question id, selected answer id 作为成员,并且当您显示用户提出的问题时在访问 selected answer id 之前选择了一些内容并将其显示为已选择。

假设你有 5 个问题,每个问题最多有 5 个选项。

您有一个带有 table 视图的屏幕,您在 table 视图中一次显示 1 个带有选项(2 - 5 行)的问题,您将为用户提供一些选项导航到下一个问题(在这种情况下,您重新加载 table 下一个问题)

这是您的设置。

现在,根据我的建议创建一个 NSObject class 来保存每个问题的所选选项的信息。

@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’s 作为 1, 2, 3, 4, 5,并且最初没有选择答案,因此为 selected answer id 保留一个不适用的 ID 作为 -1

在您的 class 中创建一个 NSMutableArray,显示 table 视图为

NSMutableArray *m_selectedAnsInfoList;

将上面的数组初始化一次为

m_selectedAnsInfoList = [NSMuatbleArray alloc] init];

最初为每个问题创建 CustomObject class 的对象

(假设我们有 5 question 所以 questionCount = 5

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];
}

您创建 table 视图,将答案显示为问题的行,最初您会将所有行显示为未选中。

里面 didSelectRowAtIndexPath:

- (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];
}

通过这种方式,您可以存储为每个问题选择的答案的信息,并根据列表中相应对象中存储的信息显示选择的答案。