Question

I have an NSMutableArray in which I insert the values when any user selects the field and this is an multi selection field so I can insert the data according to multiple selection in NSMutableArray but when user unselects an selected field then how can I remove the value of unselected field which are selected earlier.

Was it helpful?

Solution

yourviewcontroller.h

 NSMutableArray *arr_select;
 NsMutableArray *your_arry;

Yourviewcontroller.m

- (void)viewDidLoad
{
  arr_select=[[NSMutableArray alloc]init];   

   for (int i = 0; i < [your_arry count]; i++)
    {
        [arr_select addObject:@"0"];
        [arr_select retain];
    }


}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath
{
    NSLog(@"%@",arr_select);

    NSNumber *anumber = [NSNumber numberWithInteger:indexPath.row];

    if([[arr_select objectAtIndex:indexPath.row] isEqualToString:@"0"])
    {
        [arr_select replaceObjectAtIndex:indexPath.row withObject:@"1"];
        [arr_select retain];
    }
    else
    {
        [arr_select replaceObjectAtIndex:indexPath.row withObject:@"0"];
        [arr_select retain];
    }


    NSLog(@"%@",arr_select);


    [tableView reloadData];


}


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


  if([[arr_select objectAtIndex:indexPath.row]isEqualToString:@"0"])
        {

            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }

}


-(IBAction)Done:(id)sender
{
    NSLog(@"%@",arr_select);
    NSLog(@"%@",co_arr);
    arr_data = [[NSMutableArray alloc] init];

    for (int i = 0; i <[arr_select count]; i++)
    {
        if([[arr_select objectAtIndex:i] isEqualToString:@"1"])
        {
            NSLog(@"%d",i);

            [arr_data addObject:[co_arr objectAtIndex:i]];
            [arr_data retain];
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top