How to save selected rows from UITableview to NSString variable?

I am new to xcode and making my first iPhone app.

I have a UITableview with 8 rows in a tabbed screen. I have a code which let's the user select maximum 4 rows at a time and mark it with checks when selected.

Now, when I change the view and navigate to the next tab I want to save the text from these checked rows in a single NSString variable separated by commas.

Is it possible to do so? Thank you, any help is very much appreciated.

Here is the code of the first tab from where I want to save the selected rows.

@implementation Psychological

static int count = 0;
- (void)viewDidLoad {
[super viewDidLoad];

listOfItems = [[NSMutableArray alloc] init];

[listOfItems addObject:@"1 option"];
[listOfItems addObject:@"2 option"];
[listOfItems addObject:@"3 option"];
[listOfItems addObject:@"4 option"];
[listOfItems addObject:@"5 option"];
[listOfItems addObject:@"6 option"];
[listOfItems addObject:@"7 option"];
[listOfItems addObject:@"8 option"];    

}
#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
// Return the number of rows in the section.
return [listOfItems count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}


// Configure the cell...
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.text = cellValue;



return cell;
}


#pragma mark -
#pragma mark Table view delegate

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

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
    if(count < 4)
    {
    [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
    [selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
        count++;
    }

} else {
    [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
    [selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
    count --;
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];

}


- (void)dealloc {

[listOfItems release];
[super dealloc];
}


@end

Answers


the easiest way is make method wich will return string that you need. Method will look something like this:

- (NSString *) selectedItems {
    NSMutableString *result = [NSMutableString string];
    for (int i = 0; i < [itemsArray count]; i++) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
        [tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
        if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
            [result appendFormat:@"%@",cell.textLabel.text];
        }
    }
    if (result.length > 2) {
        [result replaceCharactersInRange:NSMakeRange(result.length-1, 1) withString:@""];
    }
return result;
}

To get this line in another view controller you should find Psychological view controller in navigationController.viewController and call this method.

- (void) method {
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[Psychological class]]) {
            NSString *str = vc.selectedItems;
        }
    }
}

Hope the following code suits your requirement:

//didSelect method

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

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {

     if(count < 4) {

         [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
         [selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
         count++;
     }

  } else {

    [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
    [selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
    count --;
}

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

Declare the selectedCell globally to access in all tabs

NSMutableString *selectedCell = [[NSMutableString alloc]init]; //Should declare this string globally

//Use the following code in -viewWillDisappear Method or where you feel it fits (method that called when navigating from present class)

for(int i = 0; i < [selectedIndexes count]; i++){

 [selectedCell appendString:[selectedIndexes objectAtIndex:i];
 [selectedCell appendString:@","];

}

NSRange range = {[selectedCell length]-1,1};
[selectedCell deleteCharactersInRange:range];

NSMutableString *selectedRow=[NSMutableString alloc]init];

for(int i=0; i<[count];i++)
{
    selectedRow=[arrayName objectAtIndex:row];
    [selectedRow appendString:@","];
}

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

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

    if (selectedCell.accessoryType == UITableViewCellAccessoryNone) {
if(count<4){
        selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
        [[listOfItems objectAtIndex:indexPath.row]setObject:@"YES" forKey:@"Selected"];
count++;
}

    } else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        if(count>=0){
        selectedCell.accessoryType = UITableViewCellAccessoryNone;
        [[listOfItems objectAtIndex:indexPath.row]setObject:@"NO" forKey:@"Selected"];
count--;
    }  
} 

replace above code and when you want to navigate your page at that time check whether Selected value yes or no if yes then pass that value otherwise leave it.

for (int i =0 ; i<[listOfItems count]; i++) {
        if ([[[listOfItems objectAtIndex:i]valueForKey:@"Selected"]isEqualToString:@"YES"]) {
            if ([selectedId length]==0) {
                selectedId = [NSString stringWithFormat:@"%@",[[listOfItems objectAtIndex:i]valueForKey:@"user_id"]];
            }else {
                selectedId = [selectedId stringByAppendingFormat:@",%@",[[listOfItems objectAtIndex:i]valueForKey:@"user_id"]];
            }
        }
    }

You may want to have a global array. Store the selected values into that global array then it would be easy to display them elsewhere.. :-)


Create a new class, let's call it Option, and give it two properties. One is a NSString called name and the other is a BOOL called selected. Instead of having listOfItems as an array of strings, make it a list of Option objects. Have each Option keep track of whether or not it's currently selected.

Make the controller class that owns listOfItems into a UITabBarControllerDelegate and implement tabBarController:didSelectViewController: so that it builds the string and passes it to the next selected controller.


Need Your Help

Sending Bulk Emails using PHP

php email execution bulk

I have to send mails to all users in the site when a new user joins.

Synchronizing IsSelected and SelectedItem in a ListView

.net wpf mvvm

I have some images (positions) in a WPF control. Each image correlates to an item in a ListView beneath that control. When the user clicks on a position, I want the corresponding ListViewItem to be