【问题标题】:How to add only one check mark for each section in a gropued tableview, IOS如何在 gropued tableview,IOS 中为每个部分仅添加一个复选标记
【发布时间】:2013-01-11 17:48:30
【问题描述】:

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

填充类:

#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,还是有其他方法可以做到这一点?

【问题讨论】:

  • 为什么不尝试使用带有复选标记图像的按钮的自定义单元格并制作一个通用选择器。单击时将 indexpath.row 添加到数组中。这样您将选择所有索引跨度>
  • 每个 UITableViewCell 都有一个 contentView,清理它并在里面放你想要的东西。

标签: iphone ios objective-c uitableview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    是的,这是可能的。您需要提取所有数据并准备一个 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
    

    【讨论】:

      【解决方案3】:

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

      跟踪行索引

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

      蛮力

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

      【讨论】:

        猜你喜欢
        • 2014-09-01
        • 2013-01-11
        • 2019-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-13
        • 1970-01-01
        • 2019-07-11
        相关资源
        最近更新 更多