mercredi 18 janvier 2017

Save selected check box in ios Objective c

I need to check that the parent check box is selected than all child selected button must be selected.And if parent check box is not selected than child should not be selected.I've done this with below code.

Now,There are more then one child data display and from all of them if one or more are not selected and i saved it than the result should be same as before i saved.

Below is code

    - (void)viewDidLoad {
        [super viewDidLoad];

        [_tbledemo setDataSource:self];
        [_tbledemo setDelegate:self];

        [_tblesubdemo setDataSource:self];
        [_tblesubdemo setDelegate:self];

        arrayCheckUnchek = [[NSMutableArray alloc]init];
        arraysubCheckUnchek = [[NSMutableArray alloc]init];

        cellDataArray = [[NSMutableArray alloc]initWithObjects:@"cell-1",@"cell-2",@"cell-3",@"cell-4",@"cell-5", nil];
        subData = [[NSMutableArray alloc]initWithObjects:@"ios",@"android",@"window",@"mi", nil];

        for(int i=0; i<[cellDataArray count]; i++)
        {
            [arrayCheckUnchek addObject:@"Uncheck"];
        }

        for(int i=0; i<[subData count]; i++)
        {
            [arraysubCheckUnchek addObject:@"Uncheck"];
        }

        [viewsubView setFrame:CGRectMake(10, 400, 200, 200)];
        [viewsubView setHidden:YES];
        [self.view addSubview:viewsubView];
    }
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(_tbledemo==tableView)
    {
        return cellDataArray.count;
    } else {
        return subData.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(_tbledemo==tableView)
    {
        static NSString *CellIdentifier= @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
        }

        cell.textLabel.text = [cellDataArray objectAtIndex:indexPath.row];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(100.0, 7.0, 30.0, 30.0)];

        if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
            [button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
        else
            [button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];
        [button setTag:indexPath.row];
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];


        UIButton *btnView = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnView setFrame:CGRectMake(150.0, 7.0, 100.0, 30.0)];
        [btnView setBackgroundColor:[UIColor clearColor]];
        [btnView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btnView setTitle:@"Click Me" forState:UIControlStateNormal];
        [btnView addTarget:self action:@selector(btnViewClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btnView];
        return cell;

    }
    else
    {
        static NSString *CellIdentifier= @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
        }

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setFrame:CGRectMake(100.0, 7.0, 30.0, 30.0)];
        NSString *temp ;
        for (NSString *string in arrayCheckUnchek) {
            if ([string isEqualToString:@"Check"]) {
                temp = string;
            }
            else
            {
            }
        }
        if ([temp isEqualToString:@"Check"]) {
            [button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];
        }
        else
        {
            [button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(buttonSubClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];

        cell.textLabel.text = [subData objectAtIndex:indexPath.row];
        return cell;

    }
}

-(void)btnViewClicked:(id)sender
{
    [viewsubView setHidden:NO];
    [_tblesubdemo reloadData];
    [_tbledemo reloadData];
    [self fadeInAnimation:self.view];

}
-(void)buttonSubClicked:(id)sender
{
    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:_tblesubdemo];
    NSIndexPath *indexPath = [_tbledemo indexPathForRowAtPoint:touchPoint];
    UIButton *button = (UIButton *)sender;

    if([[arraysubCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
    {
        [button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];
        [arraysubCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Check"];
    }
    else
    {
        [button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
        [arraysubCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
    }

}
-(void)buttonClicked:(UIButton *)sender
{
    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:_tbledemo];
    NSIndexPath *indexPath = [_tbledemo indexPathForRowAtPoint:touchPoint];

    UIButton *button = (UIButton *)sender;

    if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
    {
        [button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];
        [arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Check"];
    }
    else
    {
        [button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
        [arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (IBAction)btnSaveclicked:(id)sender {
    NSLog(@"tis is my array main %@",arrayCheckUnchek);
    NSLog(@"tis is my array main %@",arraysubCheckUnchek);

    [viewsubView setHidden:YES];
    [self fadeInAnimation:self.view];

}
- (IBAction)btnCancelClicked:(id)sender {
    [viewsubView setHidden:YES];
    [self fadeInAnimation:self.view];

}

-(void)fadeInAnimation:(UIView *)aView {

    CATransition *transition = [CATransition animation];
    transition.type =kCATransitionFade;
    transition.duration = 0.5f;
    transition.delegate = self;
    [aView.layer addAnimation:transition forKey:nil];
}

enter image description here

enter image description here




Aucun commentaire:

Enregistrer un commentaire