2017-11-24 16 views
1

私はそれは私が複数のテーブルビューの行を選択すると、これらの値が加算され、静的ではありません、このようNSMutableArrayの値を文字列に格納する方法は?

Selected Id : (
    2, 
    92, 
    154 
) 

をNSMutableArrayのを取得しています。

現在私は4行すなわち、1つの以上の行を選択した場合、配列は、私は、任意の行の選択を解除すると、I 2を選択解除するとし、この

Slected Id : (
    2, 
    92, 
    154, 
    12 
) 

ようになる 、そのIDがそのアレイにプリントされる3行を選択しました行、その後、新しい配列が

Slected Id : (

    154, 
    12 
) 

、私は私にこのような出力を与えるされる任意のオブジェクトでこれらの値を格納するようにするため、

になります私は、5つの値を選択し、カンマ

で区切られた3210

すなわちオブジェクトは、それはそれを実装する方法この

6,87,154,65,45 

のように格納することがありますか?

マイプロジェクトコード:

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

     self.selectedPath = indexPath; 

     if ([tableView isEditing]) { 

      // [selectedArray addObject:[_mutableArray objectAtIndex:indexPath.row]]; 

      [selectedArray addObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]]; 

      count1=(int)[selectedArray count]; 
      NSLog(@"Selected count is :%i",count1); 
      NSLog(@"Slected Id : %@",selectedArray); 


     }else{ 

     /// other action 
    } 

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

    self.selectedPath = indexPath; 


// [selectedArray removeObject:[_mutableArray objectAtIndex:indexPath.row]]; 
    [selectedArray removeObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]]; 

    count1=(int)[selectedArray count]; 
    NSLog(@"Selected count is :%i",count1); 
    NSLog(@"Slected Id : %@",selectedArray); 



    if (!selectedArray.count) { 
     [self.tableView setEditing:NO animated:YES]; 
    } 
} 

ここで私はその選択したデータを取得します、のために

NSLog(@"Slected Id : %@",selectedArray); 
+0

あなたは、現在選択されたIDの配列を取得していますか? 'Slected Idのように:(154,12)' –

+0

うん...!私は154,12 ...のような括弧や空白なしのようにしたい。 –

+0

サンプルコードを貼り付けることができます..どのように配列を形成していますか? – Nilesh

答えて

0

あなたは以下

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ 
    //your code...  
} 
、のような UITableViewのデリゲートメソッドを取る必要があり、これを行います

次に、選択可能な行IDの選択を解除します.1つの可変配列を以下のようにします。

var selectedRowArr = [String]() // Change if your id is Int or other type. 

今あなたが

let myString = selectedRowArr.joined(separator: ",") 

出力は、

1,12である必要があり、文字列に

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ //your code... // First check "rowId" already added or not if selectedRowArr.contain(rowId){ // make sure "rowId" is same type of array selectedRowArr = selectedRowArr.filter { $0 != rowId as! String } // remove object from array list } else { selectedRowArr.append(rowID) } } 

以下の変換アレイのようなものをデリゲートメソッドの上に更新する必要があります13

更新日:

まず、私はあなたが文字列に配列を変換したい場合は、

NSString * result = [selectedArray componentsJoinedByString:@","]; 

出力があるべきあなたのdidSelectRowAtIndexPath方法

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

     self.selectedPath = indexPath; 

     if ([tableView isEditing]) { 

      First you need to check "c_uid" is already added in the Array or not ? 
      if ([selectedArray containsObject:someObject]) { 
       [selectedArray removeObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]]; 

       count1=(int)[selectedArray count]; 
       NSLog(@"Selected count is :%i",count1); 
       NSLog(@"Slected Id : %@",selectedArray); 



        if (!selectedArray.count) { 
        [self.tableView setEditing:NO animated:YES]; 
        } 
      } 
      else { 
       [selectedArray addObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"c_uid"]]; 

       count1=(int)[selectedArray count]; 
       NSLog(@"Selected count is :%i",count1); 
       NSLog(@"Slected Id : %@",selectedArray); 
      } 


     }else{ 

     /// other action 
    } 

を編集しますdidDeselectRowAtIndexPath方法を削除

1,12,13

+0

私はスピーディーなコードに慣れていないので、Objective cの自分のコードに従って答えを変換できます。 –

+0

@NikitaPatil check updated – iPatel

+0

このコードをチェックさせてください。 –

0

希望は参考になりましたコードを以下になります!

NSMutableString *strProductId = [[NSMutableString alloc]init]; 
[strProductId setString:@""]; 
NSArray *arrTemp = [selectedArray copy]; 
for (int i=0; i<[arrTemp count]; i++) 
{ 
    if ([arrTemp count]-1 == i) 
    { 
     [strProductId appendFormat:@"%@",[arrTemp objectAtIndex:i]]; 
    } 
    else 
    { 
     [strProductId appendFormat:@"%@,",[arrTemp objectAtIndex:i]]; 
    } 
} 

NSLog(@"Selected Id : %@",strProductId); 

出力は次のようになります!

選択されたID:6,87,154,65,45

関連する問題