showing data in table view sections from different NSMutableArrays - iphone

I am fetching data form JSON and storing that data in 10 differnet NSMutable Arrays .I want to show that data in 10 sections of the tableview so how to do this
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
//return categories.count ;
if(section == 0)
{
return [appDelegate.books1 count];
}
else if(section == 1)
{
return [appDelegate.books2 count];
}
else if(section == 2)
{
return [appDelegate.books3 count];
}
else if(section == 3)
{
return [appDelegate.books4 count];
}
else if(section == 4)
{
return [appDelegate.books5 count];
}
else if(section == 5)
{
return [appDelegate.books6 count];
}
else if(section == 6)
{
return [appDelegate.books7 count];
}
else if(section == 7)
{
return [appDelegate.books8 count];
}
else if(section == 8)
{
return [appDelegate.books9 count];
}
else if(section == 9)
{
return [appDelegate.books10 count];
}
}
if i am doing below it shows only first section
Book1*aBook=[appDelegate.books1 objectAtIndex:indexPath.row];
int count=[appDelegate.books1 count];
cell.text = aBook.municipality;
cell.detailTextLabel.text=aBook.title;
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.backgroundColor=[UIColor clearColor];
self.tableView.backgroundColor=[UIColor clearColor];
return cell;

In my view (if you don't want to change much in your code) do it like this
make an another NSMutableArray object lets say allData
add all your array into allData according to section, somthing like this
[allDatad addObjects:appDelegate.books1,appDelegate.books2,appDelegate.books3...,appDelegate.books10,nil];
now in cellForRowAtIndexPath just change this
Book1* aBook=[[allData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
int count=[[allData objectAtIndex:indexPath.section] count];
and in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[allData objectAtIndex:indexPath.section] count];
}

Use indexPath.section to determine for which section you are preparing your cell.
For e.g., if indexPath.section is 3 then use appDelegate.books4.
Hope this helps.

A better more scalable implementation would be to add these arrays to another array, say arrayOfArrays. Now your number of sections are
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [arrayOfArrays count];
}
number of rows are:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[arrayOfArrays objectAtIndex:section] count];
}
and your cellForRowAtIndexPath is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//all the dequeueing and initialization and stuff
Book1 *aBook=[[arrayOfArrays objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
int count=[[arrayOfArrays objectAtIndex:indexPath.section] count];
cell.text = aBook.municipality;
cell.detailTextLabel.text=aBook.title;
//do other stuff
return cell;
}

You should store all those arrays in NSMutableDictionary like,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:appDelegate.books1 forKey:#"1"];
[dict setObject:appDelegate.books2 forKey:#"2"];
[dict setObject:appDelegate.books3 forKey:#"3"];
and access with the reference of indexPath.section while creating cell like,
Book1*aBook = [dict valueForKey:[NSString stringWithFormat:#"%i", indexPath.section + 1]];
and at time when you want to define numberOfRowsInSection you can get count from the NSMutableDictionary.
Book1*aBook = [dict valueForKey:[NSString stringWithFormat:#"%i", section + 1]];
[aBook count];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
Book1*aBook;
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:MyIdentifier] autorelease];
}
if(indexPath.section == 0)
{
aBook=[appDelegate.books1 objectAtIndex:indexPath.row];
}
else if(indexPath.section == 1)
{
aBook=[appDelegate.books2 objectAtIndex:indexPath.row];
}
int count=[appDelegate.books1 count];
cell.text = aBook.municipality;
cell.detailTextLabel.text=aBook.title;
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.backgroundColor=[UIColor clearColor];
self.tableView.backgroundColor=[UIColor clearColor];
return cell;
}

Related

assigning datasource to two tableviews in one view controller

Whats wrong in the following code?
here i am using two table views and assigning them with different data source, but its not working
-(void)viewDidLoad
{
[super viewDidLoad];
arryData = [[NSArray alloc] initWithObjects:#"MCA",#"MBA",#"BTech",#"MTech",nil];
arryData2 = [[NSArray alloc] initWithObjects:#"Objective C",#"C++",#"C#",#".net",nil];
flag=1;
myTable1.hidden=YES;
myTable2.hidden=YES;
btnOne.layer.cornerRadius=8;
btnTwo.layer.cornerRadius=8;
myTable1.layer.cornerRadius=8;
myTable2.layer.cornerRadius=8;
myTable1.delegate=self;
myTable1.dataSource=self;
myTable2.delegate=self;
myTable2.dataSource=self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.myTable1=tableView)
{
return [arryData count];
}
else {
return [arryData2 count];
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView==self.myTable1)
{
static NSString *CellIdentifier1 = #"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] ;
}
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
NSLog(#"1here is%i %#",indexPath.row,cell.textLabel.text);
return cell;
}
else
{
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] ;
}
cell.textLabel.text = [arryData2 objectAtIndex:indexPath.row];
return cell;
}
}
-(IBAction)btnCategory:(id)sender
{
if (flag==1) {
flag=0;
myTable1.hidden=NO;
myTable2.hidden=YES;
}
else{
flag=1;
myTable1.hidden=YES;
myTable2.hidden=YES;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
myTable1.hidden=YES;
myTable2.hidden=YES;
}
-(IBAction)btnTopic:(id)sender
{
if (flag==1) {
flag=0;
myTable2.hidden=NO;
myTable1.hidden=YES;
}
else{
flag=1;
myTable2.hidden=YES;
myTable1.hidden=YES;
}
}
</code>
when i remove datasource of mytable2 all data gets added to table1 else nothing gets loaded in none of the table.
You did mistake. See my code..
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(self.myTable1==tableView) // ----- Change here you need to write == symbol instead of = symbol
{
return [arryData count];
} else {
return [arryData2 count];
}
}
Try this,
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==myTable1)
{
return [arryData count];
}
else {
return [arryData2 count];
}
}
-(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] ;
}
if(myTable1==tableView)
{
cell.textLabel.text = [arryData objectAtIndex:indexPath.row];
NSLog(#"1here is%i %#",indexPath.row,cell.textLabel.text);
}
else
{
cell.textLabel.text = [arryData2 objectAtIndex:indexPath.row];
}
return cell;
}

Search Results not visible

I have a table view with a search display controller. When I type a search the log shows me the search is working and filtering properly however the screen only shows "No Results". I'm using Core Data if that makes a difference.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
}
else{
return[[self.fetchedResultsController sections]count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
return [secInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *CellIdentifier = #"Cell";
if (tableView != self.tableView) {
NSLog(#"Found searchDisplayController");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSLog(#"Found regular table View");
}
// Configure the cell...
Instance *instance = [self.fetchedResultsController objectAtIndexPath:indexPath];
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = instance.name;
}
Here are NumberOfRowsInSection and Number:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
}
else{
return[[self.fetchedResultsController sections]count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
return [secInfo numberOfObjects];
}
Check your implementation of numberOfSectionsInTableView and numberOfRowsInSection.
In the first, you should return 1 if it's the search table view (1 section).
In numberOfRowsInSection you should also check the given tableView argument as you did in numberOfSectionsInTableView and return [searchResults count].
Shouldn't your implementation be something like :-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return 1;
}
else
{
return[[self.fetchedResultsController sections]count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.searchDisplayController.searchResultsTableView)
{
return [searchResults count];
}
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections]objectAtIndex:section];\
return [secInfo numberOfObjects];
}
If the number of search results is high, then I would suggest using another Fetched Results Controller instead of an array.

How to implement Alphabetical section headers in a UITableView having content getting from DataBase

Till now I have implemented UITableView by populate the contents from the database
By retrieving in an array from sqlite data base
storedContactsArray = [Sqlitefile selectAllContactsFromDB];
so no multiple sections , section headers and returns storedContactsArray.count as number of rows.
Now i need to populate the same data in table view but The data set in Alpabetical sections in alphabetical order.
I tried with
alphabetsArray =[[NSMutableArray alloc]initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z",nil];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [alphabetsArray count];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return alphabetsArray;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [alphabetsArray objectAtIndex:section];
}
But in case of numberOfRowsInSection it fails since there is no contacts in storedContactsArray initially
Error occurs: -[__NSArrayM objectAtIndex:]: index 25 beyond bounds for empty array
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[storedContactsArray objectAtIndex:section] count]
}
any advice of use full links pls
To achieve your requirement you need to first separate out all the data in to section as Alphabetical order. which is as following.
Here section is a Mutable Dictionary in which we will get all data as alphabetical sets.
//Inside ViewDidLoad Method
sections = [[NSMutableDictionary alloc] init]; ///Global Object
BOOL found;
for (NSString *temp in arrayYourData)
{
NSString *c = [temp substringToIndex:1];
found = NO;
for (NSString *str in [sections allKeys])
{
if ([str isEqualToString:c])
{
found = YES;
}
}
if (!found)
{
[sections setValue:[[NSMutableArray alloc] init] forKey:c];
}
}
for (NSString *temp in arrayYourData)
{
[[sections objectForKey:[temp substringToIndex:1]] addObject:temp];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[sections allKeys]count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
}
-(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];
}
NSString *titleText = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = titleText;
return cell;
}
Please try it I am using it and it working fine
Hope it helps you !!!

how to load the items in array at run time

I have three tableView one containg cateogry one containg type and other product name i want that when user selct any category the type array should be loaded at run time getting the same type of the selected category
In my example if i select category Herbicide then it should insert the contents of the
typeHerbicides in arrayType but when i load table second it does not show any values
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView ==tblSimpleTable )
return [categoryArray count];
else if(tableView==tblSimpleTableType)
return [typeArray count];
else if(tableView==tblSimpleTableProduct)
return [productArray count];
else if(tableView==tblSimpleTableOneSection)
return [categorySectionOneArray count];
else if(tableView==tblSimpleTableTypeSection)
return [typeArraySection count];
else
return [productArraySection 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] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if (tableView ==tblSimpleTable ) {
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [categoryArray objectAtIndex:indexPath.row];
return cell;
}
else if (tableView==tblSimpleTableType){
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [typeArray objectAtIndex:indexPath.row];
return cell;
}
else if(tableView==tblSimpleTableProduct) {
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [productArray objectAtIndex:indexPath.row];
return cell;
}
else if(tableView==tblSimpleTableOneSection){
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [categorySectionOneArray objectAtIndex:indexPath.row];
return cell;
}
else if(tableView==tblSimpleTableTypeSection){
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [typeArraySection objectAtIndex:indexPath.row];
return cell;
}
else {
cell.textLabel.font=[UIFont fontWithName:#"Arial" size:16];
cell.textLabel.text = [productArraySection objectAtIndex:indexPath.row];
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView ==tblSimpleTable ) {
titleCategory=[categoryArray objectAtIndex:indexPath.row];
NSLog(#"This is titleCategory Selected %#",titleCategory);
NSString*test=#"Herbicides";
if([titleCategory isEqualToString:test]){
typeHerbicides=[[NSArray alloc] initWithObjects:#"ACCLAIM EXTRA",#"ILLOXAN",#"REVOLVER",nil];
typeArray=[[NSArray alloc] initWithArray:typeHerbicides copyItems:YES];
}
else {
typeArray=[[NSArray alloc] initWithObjects:#"ILLOXAN",#"REVOLVER",nil];
typeArray=[[NSArray alloc] initWithArray:typeHerbicides copyItems:YES];
}
}
else if(tableView==tblSimpleTableType)
titleType=[typeArray objectAtIndex:indexPath.row];
else if(tableView==tblSimpleTableProduct)
titleProduct=[productArray objectAtIndex:indexPath.row];
else if(tableView==tblSimpleTableOneSection)
titleSectionOneCategory=[categorySectionOneArray objectAtIndex:indexPath.row];
else if(tableView==tblSimpleTableTypeSection)
titleTypeSection=[typeArraySection objectAtIndex:indexPath.row];
else
titleProductSection=[productArraySection objectAtIndex:indexPath.row];
}
When user selects any category then in didSelectRowAtIndexPath you can initialize your typeArray based on category selected:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == tblSimpleTableType)
{
//initialize typeArray;
//Once typeArray is initialized just reload the tableview as follows:
[tblSimpleTableType reloadData];
}
}
Just try by returning the cell at last of the function only and see.

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
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.