简体   繁体   中英

How to store values from NSMutableArray to string?

I am getting an NSMutableArray like this,

Selected Id : (
    2,
    92,
    154
)

It is not static, these value are added when I select multiple table view rows.

Currently I selected 3 rows whose id is printing in that array, If I select one more row ie 4 rows then array will be like this

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

If I deselect any row, suppose I deselect 2 rows, then new array will be

Slected Id : (

    154,
    12
)

So that, I want to store these values in any objects which will be give me output like this,

154,12

ie objects separated by comma

I selected 5 values then, it has to store like this

6,87,154,65,45

How to implement it ?

My Project Code :

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

Here I will get that selected data,

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

For do this you should take below UITableView 's delegate methods like,

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
  //your code...      
 }

Now manage select and deselect row id by taking one mutable Array like below

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

now you need to update above those delegate methods like below

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

Convert Array to string

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

Output should be

1,12,13

Updated :

First remove didDeselectRowAtIndexPath method I will edit your didSelectRowAtIndexPath method

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

If you want to convert array to string then

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

Output should be

1,12,13

Hope will helpful to you following code!

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);

Output might be!

Selected Id : 6,87,154,65,45

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM