【问题标题】:How to perform Validation in ios uitableview (group table)?如何在 ios uitableview(组表)中执行验证?
【发布时间】:2012-12-12 08:19:22
【问题描述】:

在我的应用程序中,我使用了 UItableview 组表。 其中有两个部分,我想对其进行验证。

例如:在第 1 节中有两行,如果用户选择第 2 行,则用户必须从第 2 节中选择一些值,如果用户在第 1 节中选择了第二行,则无需在第 2 节中进行选择。

以下是我的代码:

- (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];
}

【问题讨论】:

  • 现在有什么问题?
  • 我想根据用户选择进行验证
  • @Nilesh.S.Joshi 我想你想,当第一个用户点击第 1 部分的“是”时,用户可以选择第 2 部分,对吗?然后看看下面我的答案

标签: iphone objective-c ios xcode ios5


【解决方案1】:

在UITableView 的didselect 方法中,您需要检查节号然后检查从该节中单击了哪一行

    - (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
    }
    }

【讨论】:

    【解决方案2】:
    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
    
      }
    
    }
    

    【讨论】:

      【解决方案3】:

      您可以先声明一个变量 count 以跟踪在第 2 节中选择了多少项目,并将 isItemRequired 作为标志变量和 sythesize 它们,然后使用这些委托:

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

      开头的示例代码:

      - (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];
              }
      
          }
      
      
      }
      

      【讨论】:

        【解决方案4】:

        在下面的委托中

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

        在委托返回的 indexPath 的帮助下添加一个检查,它会告诉你选择了哪个部分的哪一行:

        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.
        }
        

        【讨论】:

          【解决方案5】:

          首先在.h 文件中取一个BOOL 变量和isYes,如下所示...

          BOOL isYes;
          

          在viewDidLoad: 方法之后,只需将其分配给NO,如下所示..

          isYes = NO;
          

          之后在didSelectRowAtIndexPath 委托方法中使用如下...

          - (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  
                      }
                   }
          } 
          

          希望对你有帮助....

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-05-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多