2017-05-11 21 views
0

我已經編寫了用於顯示帶有3個部分的tableview的代碼,現在我想將選定的行項存儲到一個數組中,並且當我嘗試了下面的代碼時,數組值爲零。建議我該如何解決這個問題 下面我加入的代碼和JSON樣本數據如何在tableview中將多個選定的行項存儲在可變數組中

{ 
 
    "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); 
 
     
 
    } 
 
}

+0

具有u初始化myArray的? –

+0

是的,我已經初始化@hamentmiglani –

+0

你在哪裏得到零?如果您在初始化數組時遇到問題,那麼這與tableview函數無關。你確認你的數組是否正確填充? – Russell

回答

1
  1. 分配storeArray = [[NSMutableArray alloc]init];首先創建一個出口用於陣列和在.M

  2. 現在合成初始化所述陣列中的viewDidLoad中

  3. 現在實行的cellForRowAtIndexPath和didSelectRowAtIndexPath

    查找代碼並讓我知道u必須要澄清

String_Name = [ArrayName objectAtIndex:indexPath.row]; 
 

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

 
     [myArray addObject:String_Name]; 
 

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

1

試試這個代碼

你應該創建之前新的NSMubaleArray *storeArray; NSMutableArray在viewDidLoad

-(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); 
} 
相關問題