1

我从 .plist 填充分组表视图。我只想为每个部分放置一个复选标记。我尝试使用 anNSMutableArray来保留选定的行并显示复选标记,但没有成功。

在此处输入图像描述

填充类:

#import <Foundation/Foundation.h>

@interface ParsePlist : NSObject

@property (nonatomic,strong) NSDictionary * agendaDic;
- (void)passStringToAgendaDic:(NSString *)string;

//survey questions
-(NSArray*)getSurvey;
-(NSString*)getSurveyQuestion:(NSInteger ) section ;

//answers
-(NSArray*)getSurveyAnswers:(NSInteger) section;
-(NSString*)getQuestionAnswer:(NSInteger ) section Row:(NSInteger) row ;

复选标记代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [_parsePlist getQuestionAnswer:[indexPath section] Row:[indexPath row]]; // get string in survey

    if([_selectedIndexPathArray containsObject:[_parsePlist getQuestionAnswer:[indexPath section] Row:[indexPath row]]])
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // this for trying check mark
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        //remove index path
        [_selectedIndexPathArray removeObject:[_parsePlist getQuestionAnswer:[indexPath section] Row:[indexPath row]]];
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [_selectedIndexPathArray addObject:[_parsePlist getQuestionAnswer:[indexPath section] Row:[indexPath row]]];
    }

}

在这种情况下,如何跟踪选定的行?我应该使用NSDictionary,还是有其他方法可以做到这一点?

4

3 回答 3

0

要仅选择一个(且仅一个)行,您有 2 个选项:

跟踪行索引

有一个私有控制器 int 变量(初始化为 -1),用于存储当前选定的行索引。然后didSelectRowAtIndexPath,如果 int 变量 > -1,则首先取消选择该索引处的行。

蛮力

在开始时didSelectRowAtIndexPath循环遍历所有行并取消选择它们。它非常快,即使是一百行也应该没问题。

于 2014-08-13T16:55:15.007 回答
0

对的,这是可能的。您需要提取所有数据并准备一个 NSMutableArrays 并放置它。因此,您的问题放置在部分标题中,您的答案选项放置在每个部分的行中。

[在这里查看图片][1]

 #import "Question.h"

@interface Question ()
{
    NSMutableString *urlLString;
    NSDictionary *dataDic;
    NSArray *dataArray;
    NSMutableArray *nmA;
    NSString *question;
    NSArray *optionArray;
    NSMutableArray *pulledOptionArray;
    NSMutableArray *pulledOptionArray1;
    NSMutableArray *pulledOptionArray2;
    NSMutableArray *pulledOptionArray3;
    NSMutableArray *pulledOptionArray4;
    NSMutableArray *pulledOptionArray5;
    NSDictionary *optionDictionary;

    //split
    NSArray *secondsplit;
    int i,j;
}
@end

@implementation Question
@synthesize checkedIndexPath;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];nmA=[[NSMutableArray alloc] init];
    pulledOptionArray=[[NSMutableArray alloc] init]; pulledOptionArray1=[[NSMutableArray alloc] init];
    pulledOptionArray2=[[NSMutableArray alloc] init]; pulledOptionArray3=[[NSMutableArray alloc] init];
     pulledOptionArray4=[[NSMutableArray alloc] init]; pulledOptionArray5=[[NSMutableArray alloc] init];
    [scroll setScrollEnabled:YES];

    //[[[[[self tabBarController] viewControllers] objectAtIndex:1] tabBarItem] setEnabled:false];

    inObj=[inputData globalData];

    urlLString=[NSMutableString stringWithFormat:@"URl Goes Here"];
    //urlLString=[urlLString1 appendString:inObj.languageTestURL];

    if ([inObj.languageTestURL isEqualToString:@"C"])
    {
        [urlLString appendString:@"C"];
    }
    else if([inObj.languageTestURL isEqualToString:@"C++"])
    {
        [urlLString appendString:@"C++"];
    }
    else if([inObj.languageTestURL isEqualToString:@"java"])
    {
        [urlLString appendString:@"java"];
    }

    [page setCurrentPage:0];
    NSLog(@"First");
    [page setCurrentPageIndicatorTintColor:[UIColor blueColor]];

    NSError *erro;

    NSData *jsonData=[NSData dataWithContentsOfURL:[NSURL URLWithString:urlLString]];
    NSDictionary *jLdic=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&erro];

    NSArray *pullFromDic=[jLdic objectForKey:@"Questions"];
    NSLog(@"%@",inObj.experience);

    if ([inObj.experience isEqualToString:@"0-6 months[Fresher]"])
    {
        for (int k=0; k<=4; k++)
        {
            dataDic=[pullFromDic objectAtIndex:k];
            question=[dataDic objectForKey:@"Question"];
            optionArray=[dataDic objectForKey:@"Options"];
            for (NSDictionary *nit in optionArray)
            {
                NSString *option=[nit objectForKey:@"option"];
              //  NSLog(@"%@",option);
                [pulledOptionArray addObject:option];
            }

            [nmA addObject:question];
        }
        NSLog(@"%lu",(unsigned long)nmA.count);
    }
    else   if ([inObj.experience isEqualToString:@"1-2 year[Experienced]"])
    {
        for (int k=5; k<=9; k++)
        {
            dataDic=[pullFromDic objectAtIndex:k];
            NSLog(@"%@",dataDic);
        }

        NSLog(@"%@",dataDic);
    }
    else   if ([inObj.experience isEqualToString:@"2+ [Well Experienced]"])
    {
        for (int k=10; k<=14; k++)
        {
            dataDic=[pullFromDic objectAtIndex:k];
            NSLog(@"%@",dataDic);
        }

        NSLog(@"%@",dataDic);
    }
  // [self pullOPtions];
    [self splitArray];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)page:(UIPageControl *)sender
{

    if (sender.currentPage==0)
    {


    }
    if (sender.currentPage==1) {

        NSLog(@"Hello second");
    }
    if (sender.currentPage==2)
    {
        NSLog(@"Hello Third");
    }
}

#pragma tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // return DataFromSource.count;
    return (nmA.count-1);
   // return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellID=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
    }
    // cell.textLabel.text=[DataFromSource objectAtIndex:indexPath.row];
    if (indexPath.section==0)
    {
        cell.textLabel.text=[pulledOptionArray1 objectAtIndex:indexPath.row];
    }
    else if (indexPath.section==1)
    {
        cell.textLabel.text=[pulledOptionArray2 objectAtIndex:indexPath.row];
    }
    else if (indexPath.section==2)
    {
        cell.textLabel.text=[pulledOptionArray3 objectAtIndex:indexPath.row];
    }
    else if (indexPath.section==3)
    {
        cell.textLabel.text=[pulledOptionArray4 objectAtIndex:indexPath.row];
    }
    else if (indexPath.section==4)
    {
        cell.textLabel.text=[pulledOptionArray5 objectAtIndex:indexPath.row];
    }
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [nmA count];
}
/*- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [nmA objectAtIndex:section];
}*/
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* customView = [[UIView alloc] init];
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.opaque = YES;
    headerLabel.textColor = [UIColor whiteColor];

    headerLabel.highlightedTextColor = [UIColor whiteColor];

    headerLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f];
    headerLabel.shadowOffset = CGSizeMake(0.0f, -1.0f);

    headerLabel.font = [UIFont boldSystemFontOfSize:16];
    headerLabel.frame = CGRectMake(10.0, 0.0, 232.0,40.0);

    headerLabel.numberOfLines=2;
    headerLabel.text=[nmA objectAtIndex:section];

    [customView setBackgroundColor:[UIColor colorWithRed:111.0f/255.0f green:168.0f/255.0f blue:220.0f/255.0f alpha:1.0]];
    [customView addSubview:headerLabel];

    return  customView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%d",indexPath.section);
    UITableViewCell *cell;

    switch (indexPath.section)
    {
        case 0:
            if (cell.accessoryType==UITableViewCellAccessoryCheckmark) {
                cell.accessoryType=UITableViewCellAccessoryNone;

            }
            else{
                cell.accessoryType=UITableViewCellAccessoryCheckmark;
                NSLog(@"%@",[pulledOptionArray1 objectAtIndex:indexPath.row]);
            }
            break;
        case 1:
            NSLog(@"%@",[pulledOptionArray2 objectAtIndex:indexPath.row]);
            break;
        case 2:
            NSLog(@"%@",[pulledOptionArray3 objectAtIndex:indexPath.row]);
            break;
        case 3:
            NSLog(@"%@",[pulledOptionArray4 objectAtIndex:indexPath.row]);
            break;
        case 4:
            NSLog(@"%@",[pulledOptionArray5 objectAtIndex:indexPath.row]);
            break;

        default:
            break;
    }

}
- (NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    for ( NSIndexPath* selectedIndexPath in taBle.indexPathsForSelectedRows ) {
        if ( selectedIndexPath.section == indexPath.section )
            [taBle deselectRowAtIndexPath:selectedIndexPath animated:NO] ;
    }
    return indexPath ;
}
/*-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [taBle cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}*/

#pragma table Row metrics
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (UIFont *)fontForCell
    {
        return [UIFont boldSystemFontOfSize:18.0];
    }

-(void)splitArray
{
   // NSLog(@"%@",pulledOptionArray);
    for (int m=0; m<4; m++)
    {
        NSString *nic=[pulledOptionArray objectAtIndex:m];
        [pulledOptionArray1 addObject:nic];
    }
    for (int p=4; p<8; p++)
    {
        NSString *nic=[pulledOptionArray objectAtIndex:p];
        [pulledOptionArray2 addObject:nic];
    }
    for (int p=8; p<12; p++)
    {
        NSString *nic=[pulledOptionArray objectAtIndex:p];
        [pulledOptionArray3 addObject:nic];
    }
    for (int p=12; p<16; p++)
    {
        NSString *nic=[pulledOptionArray objectAtIndex:p];
        [pulledOptionArray4 addObject:nic];
    }
    for (int p=16; p<20; p++)
    {
        NSString *nic=[pulledOptionArray objectAtIndex:p];
        [pulledOptionArray5 addObject:nic];
    }
}
@end


  [1]: http://i.stack.imgur.com/JU0hi.png
于 2014-02-13T07:41:00.110 回答
0

因为UITableViewCells 可以重复使用,所以您应该在选择 check in 之前取消设置该附件cellForRowAtIndexPath:

cell.accessoryType = UITableViewCellAccessoryNone;
if([_selectedIndexPathArray containsObject:[_parsePlist getQuestionAnswer:[indexPath section] Row:[indexPath row]]]) {
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
于 2013-01-11T19:06:36.003 回答