Nilesh .S. Joshi
Nilesh .S. Joshi

Reputation: 567

How to perform Validation in ios uitableview (group table)?

In my app i have used UItableview group table. In which there are two section and i want to perform validation on it.

for eg:In section 1 there are two row's,if user select row one user have to select some value from section 2 and if user have select second row in 1 section then no need of selection in section two.

following is my code:

- (void)viewDidLoad
{
    NSArray *arr_data1 =[[NSArray alloc]initWithObjects:@"Yes",@"No",nil];
    NSArray *arr_data2 =[[NSArray alloc] initWithObjects:@"Monotherapy",@"Adjunctive",nil];

    NSDictionary *temp =[[NSDictionary alloc]
                         initWithObjectsAndKeys:arr_data1,@"",arr_data2,
                         @"Was it monotherapy or adjunctive",nil];
    self.tableContents =temp;
    self.arr_data =[[self.tableContents allKeys]
                      sortedArrayUsingSelector:@selector(compare:)];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.arr_data count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.arr_data objectAtIndex:section];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *listData =[self.tableContents objectForKey:
                        [self.arr_data objectAtIndex:section]];
    return [listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    NSArray *listData =[self.tableContents objectForKey:
                        [self.arr_data objectAtIndex:[indexPath section]]];

    UITableViewCell * cell = [tableView
                              dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

    if(cell == nil) {



cell = [[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:SimpleTableIdentifier];


    }

NSUInteger row = [indexPath row];

cell.textLabel.text = [listData objectAtIndex:row];

return cell;

}



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

    NSArray *listData =[self.tableContents objectForKey:
                        [self.arr_data objectAtIndex:[indexPath section]]];
    NSUInteger row = [indexPath row];
    NSLog(@"fdfdfdf=%d",[indexPath row]);
    NSString *rowValue = [listData objectAtIndex:row];

    if ([rowValue isEqualToString:@"Yes"])
    {
        NSString *message = [[NSString alloc] initWithFormat:@"%@",rowValue];
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"You selected"
                              message:message delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }



    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Upvotes: 0

Views: 1108

Answers (5)

user1873574
user1873574

Reputation: 93

You can first declare a variable count to keep track of how many items are selected in section 2 and isItemRequired as a flag variable and sythesize them, then use these delegates:

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

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

A sample code to begin with:

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

    if(indexPath.section == 0)
    {
        if(indexPath.row == 0)
        {
            isItemRequired = YES;

            if(isItemRequired && count==0)
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Item Required" message:@"You must have to select one or more item in section 2" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil ];

                [alert show];
            }

        }
        else
        {
            isItemRequired = NO;
        }

    }
    else if(indexPath.section == 1)
    {
        count = 0;

        for (int i=0; i < [tableView numberOfRowsInSection:1]; i++)
        {
          UITableViewCell *cell =  [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
          if(cell.selected==YES)
          {
              count++;
          }
        }

    }

}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if(indexPath.section == 1)
    {
        count = 0;

        for (int i=0; i < [tableView numberOfRowsInSection:1]; i++)
        {
            UITableViewCell *cell =  [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1]];
            if(cell.selected==YES)
            {
                count++;
            }
        }

        if(isItemRequired && count==0)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Item Required" message:@"Atleast one item should be selected in section 2" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil ];

            [alert show];
        }

    }


}

Upvotes: 1

Paras Joshi
Paras Joshi

Reputation: 20541

First Take one BOOL variable with isYes in .h file like bellow...

BOOL isYes;

after in viewDidLoad: method just assign it to NO like bellow..

isYes = NO;

after that in didSelectRowAtIndexPath delegate method just use like bellow...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
         if(indexPath.section == 0 && indexPath.row == 0)
         {
             isYes = YES;   
         }
         else if(indexPath.section == 0 && indexPath.row == 1)
         {
             isYes = NO;
         }
         if(isYes){
            if(indexPath.section == 1 )
            {
              //here user can select the section 2  
            }
         }
} 

i hope this help you....

Upvotes: 0

superGokuN
superGokuN

Reputation: 1424

In the following delegate

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

add a check with the help of indexPath returned by the delegate, it will tell you that which section's which row is selected like :

if(indexPath.section == 0 && indexPath.row == 0)
{
 // Select something from the another section also
}
else if(indexPath.section == 0 && indexPath.row == 1)
{
 // No need to select from another section.
}

Upvotes: 0

Girish
Girish

Reputation: 4712

In the didselect method of UITableView, you needs to check the section number & then check which row is clicked from that section

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

       if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
         //do this
        }
        else if (indexPath.row == 1)
        {
         //do this
        }
    }
    else
    {
    //do this
    }
    }

Upvotes: 0

Abz Ios
Abz Ios

Reputation: 64

In - (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath ,
you can check the section you need by using the following code:

- (void)tableView:(UITableView *)tableView 
  didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
  if(indexPath.section == 0)
  {
   //do as per you requirement

  }
  else if(indexPath.section == 1)
  {
   //do as per you requirement

  }

}

Upvotes: 1

Related Questions