How to get all the checked values from the Table view in iOS

Author - Webner
ios_logo

Table view is not like listview on which you can iterate over cells and get values. Tableview in iOS has reusable cells i.e. every time you scroll it, old cells don’t go up or down but the existing cells are reused to show new data. You can’t even loop through all the checked items as loop will only get values for the currently visible cells.

So the hack here is to manipulate the Tableview datasource itself. UITableView is not to provide data but to present data (that’s why it asks for the datasource in order to present the cells). Therefore since the data is already provided by your datasource, you will have to maintain checked state in that set and not in tableview. You will have to change the state of datasource for checked values at each checkbox click and keep reloading the data into the tableview, something like this:

 if (!cell.ItemSelected)
{
           cell.ItemSelected = TRUE;
           [cell.checkBoxButton setBackgroundImage:[UIImage imageNamed:@"tick.png"]  forState:UIControlStateNormal];
           NSString *service = [NSString stringWithFormat:@"%@,€%@|||", label.text,textfield.text];
           [selectedServices addObject:service];
       [self.priceList replaceObjectAtIndex:button.tag withObject:textfield.text];
       priceListArray = self.priceList;
       [self.checked replaceObjectAtIndex:btn.tag withObject:@"true"];
       checkedArray = self.checked;
   }
 else
 {
       cell.ItemSelected = FALSE;
       [cell.checkBoxButton setBackgroundImage:[UIImage imageNamed:@"box.png"] forState:UIControlStateNormal];
       NSString *service = [NSString stringWithFormat:@"%@,€%@|||", label.text,textfield.text];
       [selectedServices removeObject:service];
       NSLog(@"selectedServices: %@", selectedServices);
       [self.priceList replaceObjectAtIndex:button.tag withObject:textfield.text];
       priceListArray = self.priceList;
       [self.checked replaceObjectAtIndex:btn.tag withObject:@"false"];
       checkedArray = self.checked;
   }
   [self.barberPriceList reloadData];

Hope it helps.

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need any Android or iOS Mobile App developed please contact us at mobile@webners.com

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *