如何在tableview中的可变数组中存储多个选定的行项

时间:2017-05-11 06:25:30

标签: ios objective-c iphone json uitableview

我编写了用于显示包含3个部分的tableview的代码,现在我想将选定的行项存储到数组中,当我尝试下面的代码时,我得到的数组值为nil。建议我如何解决这个问题  下面我要添加代码和Json示例数据



{
  "technicians": {
    "group": [
      {
        "first_name": "First",
        "last_name": "Available",
        "id": ""
      },
      {
        "first_name": "idea",
        "last_name": "Chandra",
        "id": 2885
      },
      {
        "first_name": "manson",
        "last_name": "Tolios",
        "id": 2878
      },
      {
        "first_name": "tata",
        "last_name": "Masson",
        "id": 2869
      }
    ],
    "client": [
      {
        "first_name": "tsms",
        "last_name": "ysys",
        "id": 2875
      },
      {
        "first_name": "Oscar",
        "last_name": "tata",
        "id": 2851
      },
      {
        "first_name": "Peter",
        "last_name": "yaha",
        "id": 2873
      },
      {
        "first_name": "iaka",
        "last_name": "adad",
        "id": 2847
      },
      {
        "first_name": "taga",
        "last_name": "uaja",
        "id": 2917
      }
    ],
    "contractors": [
      {
        "first_name": "taha"
      },
      {
        "first_name": "haka"
      },
      {
        "first_name": "oala"
      }
    ]
  }
}






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

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"EcsStafflist"];
        if(!cell)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
    
        }
        UILabel *Fname = (UILabel *)[cell viewWithTag:18];
    
        UILabel *Lname = (UILabel *)[cell viewWithTag:19];
    
        Fname.text = [[[mydic objectForKey:[MysectionTitles objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]valueForKey:@"first_name"];
    
        Lname.text = [[[mydic objectForKey:[MysectionTitles objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]valueForKey:@"last_name"];
    
        if (indexPath.row %2 ==1)
    
            cell.backgroundColor = [UIColor colorWithRed:0.60 green:0.60 blue:0.95 alpha:1.0];
    
        else
    
            cell.backgroundColor = [UIColor colorWithRed:0.64 green:0.89 blue:0.61 alpha:1.0];

        return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  stringId = [[[mydic objectForKey:[MysectionTitles objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row]objectForKey:@"id"];

    if
        ([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
    {
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
        
    }else{
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

        [myArray addObject:stringId];

        NSLog(@"myArray %@",myArray);
        
    }
}




1 个答案:

答案 0 :(得分:1)

试试此代码

NSMubaleArray *storeArray;

中创建分配storeArray = [[NSMutableArray alloc]init];的新viewDidLoad NSMutableArray之前
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            if ([storeArray containsObject:[mainArray objectAtIndex:indexPath.row]])
            {
                [storeArray removeObject:[mainArray objectAtIndex:indexPath.row]];
            }else{
            [storeArray addObject: [mainArray objectAtIndex:indexPath.row]];
            }
            break;
        case 2:
            if ([storeArray containsObject:[mainArray objectAtIndex:indexPath.row]])
            {
                [storeArray removeObject:[mainArray objectAtIndex:indexPath.row]];
            }else{
                [storeArray addObject: [mainArray objectAtIndex:indexPath.row]];
            }
            break;
        case 3 :
            if ([storeArray containsObject:[mainArray objectAtIndex:indexPath.row]])
            {
                [storeArray removeObject:[mainArray objectAtIndex:indexPath.row]];
            }else{
                [storeArray addObject: [mainArray objectAtIndex:indexPath.row]];
            }
            break;
        default:
            break;
    }

    NSLog(@"%@",storeArray);
}