2016-06-01 41 views
0

我想处理多个行选择,即不超过3个数据来自服务(URL)的地方。下面是我尝试过的代码的一部分,但不能处理多行选择。你能抽出一些时间来解决它吗? TIA如何处理来自服务的多行数据选择

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

    NSUInteger row = [indexPath row]; 
    static NSString *MyIdentifier = @"tableCell"; 
    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

     for (id currentObject in topLevelObjects){ 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
       cell = (CustomCell *) currentObject; 
      } 
     } 
    } 

    NSDictionary *thisRow = [self.professionArr objectAtIndex:row]; 

    if(_WSConstProfessionID !=nil && ![_WSConstProfessionID isEqual:@"0"] && ![_WSConstProfessionID isEqual:@""] && _WSConstProfessionSelectedIndex ==row ){ 
     cell .accessoryType=UITableViewCellAccessoryCheckmark; 
    } else { 
     cell .accessoryType=UITableViewCellAccessoryNone; 
    } 

    cell.lblTitle.text = [thisRow objectForKey:_WSColumnProfessionName]; 
    NSString *str=[thisRow objectForKey:_WSColumnProfessionID]; 
    NSString *stra=_WSConstProfessionID; 
    if ([str isEqualToString:stra]) { 
     cell .accessoryType=UITableViewCellAccessoryCheckmark; 
     cell.highlighted=YES; 
    } else { 
     cell .accessoryType=UITableViewCellAccessoryNone; 
    } 

    return cell; 
} 

#pragma mark Table view delegate 

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    NSUInteger row = [indexPath row]; 
    NSDictionary *thisRow=[self.professionArr objectAtIndex:row]; 
    NSLog(@"%@",[thisRow description]); 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

    if (cell.accessoryType == UITableViewCellAccessoryNone){ 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 


    if(_WSConstProfessionID!=nil && ![_WSConstProfessionID isEqual:@"0"] && ![_WSConstProfessionID isEqual:@""] &&_WSConstProfessionSelectedIndex!=row){ 

     UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_WSConstProfessionSelectedIndex inSection:0]]; 
     if (cell != nil){ 
      cell .accessoryType=UITableViewCellAccessoryNone; 
     } 
    } 

    if(cell.accessoryType == UITableViewCellAccessoryCheckmark){ 
     _WSConstProfessionID=[thisRow objectForKey:_WSColumnProfessionID]; 
     _WSConstProfessionName=[thisRow objectForKey:_WSColumnProfessionName]; 
     _WSConstProfessionSelectedIndex=row ; 
    } else { 
     [email protected]"0"; 
     [email protected]"Select"; 
     _WSConstProfessionSelectedIndex=-1 ; 
    } 

    [self.navigationController popViewControllerAnimated:YES]; 
} 
+0

可以显示professionArr值?你希望用户可以选择最多3行。 –

回答

0

请设置这条线在viewDidLoad方法

tableView.allowsMultipleSelectionDuringEditing = true 

加入这行应用下面的代码后

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

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

NSUInteger row = [indexPath row]; 
NSDictionary *thisRow=[self.professionArr objectAtIndex:row]; 
NSLog(@"%@",[thisRow description]); 

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

if (cell.accessoryType == UITableViewCellAccessoryNone){ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} else { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 


if(_WSConstProfessionID!=nil && ![_WSConstProfessionID isEqual:@"0"] && ![_WSConstProfessionID isEqual:@""] &&_WSConstProfessionSelectedIndex!=row){ 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_WSConstProfessionSelectedIndex inSection:0]]; 
    if (cell != nil){ 
     cell .accessoryType=UITableViewCellAccessoryNone; 
    } 
} 

if(cell.accessoryType == UITableViewCellAccessoryCheckmark){ 
    _WSConstProfessionID=[thisRow objectForKey:_WSColumnProfessionID]; 
    _WSConstProfessionName=[thisRow objectForKey:_WSColumnProfessionName]; 
    _WSConstProfessionSelectedIndex=row ; 
} else { 
    [email protected]"0"; 
    [email protected]"Select"; 
    _WSConstProfessionSelectedIndex=-1 ; 
} 

[self.navigationController popViewControllerAnimated:YES]; 
} 
0

PLZ使用此代码 heare我把NSMutableArray数组名categoryArray

将此数组分配到viewdidload()

categoryArray = [NSMutableArray array]; 
categoryArray

我添加了_WSColumnProfessionName IDS和处理coditions。

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


      NSUInteger row = [indexPath row]; 
      static NSString *MyIdentifier = @"tableCell"; 
      CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

      if (cell == nil) { 
       NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

       for (id currentObject in topLevelObjects){ 
        if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
         cell = (CustomCell *) currentObject; 
        } 
       } 
      } 

      cell.lblTitle.text = [thisRow objectForKey:_WSColumnProfessionName]; 
      if (categoryArray.count == 0) 
      { 
       NSLog(@"category array is empty."); 
      } 
      else 
      { 
       NSString *s = [NSString stringWithFormat:@"%@",[[save_cat_array objectAtIndex:indexPath.row] valueForKey:@"category_id"]]; 
       for (int p =0 ; p<categoryArray.count; p++) 
       { 

        NSString *b =[NSString stringWithFormat:@"%@",[categoryArray objectAtIndex:p]]; 
        if ([s isEqualToString:b]) 
        { 
         cell.accessoryType = UITableViewCellAccessoryCheckmark; 
         cell.lblTitle.textt = [[self.professionArr objectAtIndex:indexPath.row]valueForKey:_WSColumnProfessionName]; 
         break; 
        } 
        else 
        { 
         cell.lblTitle.text = [[self.professionArr objectAtIndex:indexPath.row]valueForKey:_WSColumnProfessionName]; 
         cell.accessoryType = UITableViewCellAccessoryNone; 
        } 
       } 

      } 

      return cell; 
     } 
     @catch (NSException *exception) { 

     } 
    } 

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


      [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

      UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; 

      if (cell.accessoryType == UITableViewCellAccessoryNone) 
      { 
       [categoryArray addObject:[[self.professionArr objectAtIndex:indexPath.row] valueForKey:_WSColumnProfessionID]; 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 
      else 
      { 

       cell.accessoryType = UITableViewCellAccessoryNone; 
       NSString *a = [NSString stringWithFormat:@"%@",[self.professionArr objectAtIndex:indexPath.row] valueForKey:_WSColumnProfessionID]]; 
       for (int i=0;i<categoryArray.count;i++) 
       { 
        if ([a isEqualToString:[NSString stringWithFormat:@"%@",[categoryArray objectAtIndex:i]]]) 
        { 
         [categoryArray removeObjectAtIndex:i]; 
        } 
       } 

      } 

     } 
     @catch (NSException *exception) { 

     } 
    } 

你也可以处理categoryArray.count ==基地最多3个条件3

相关问题