How to save nsdictionary subtitle in mainview based on tableviewcell selection

I am currently parsing some xml that looks like this:

<Rows> <Row MANUFACTURERID="76" MANUFACTURERNAME="Fondont" ISMANU="F" ISAUTO="F"/> <Row MANUFACTURERID="18" MANUFACTURERNAME="Anti" ISMANU="T" ISAUTO="T"/> </Rows> 

I analyze it so that there is an array of dictionaries (each dictionary contains four string values โ€‹โ€‹in it).

Then I pass the ManufacturerName name to my startSortingTheArray method like this

 if (dataSetToParse == @"ICMfg") // ICMfg is a string passed to this view from the parent view cell selection enabling me to pass different data sets to this view { //Filter results (ISAUTO = T) NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"ISAUTO",@"T"]; NSArray *filteredArray = [myDataArray filteredArrayUsingPredicate:predicate]; //Passes Manufacturer strigs over to startSortingtheArray method [self startSortingTheArray:[filteredArray valueForKey:@"MANUFACTURER"]]; } 

So, here all producer names are sent to my method as an array of strings. Then I use this array to configure all my partitions / index scroller. The method below shows how I do it.

 //method to sort array and split for use with uitableview Index - (IBAction)startSortingTheArray:(NSArray *)arrayData { //If you need to sort incoming array alphabetically use this line of code //TODO: Check values coming in for capital letters and spaces etc sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; //If you want the standard array use this code //sortedArray = arrayData; self.letterDictionary = [NSMutableDictionary dictionary]; sectionLetterArray = [[NSMutableArray alloc] init]; //Index scrolling Iterate over values for future use for (NSString *value in sortedArray) { // Get the first letter and its associated array from the dictionary. // If the dictionary does not exist create one and associate it with the letter. NSString *firstLetter = [[value substringWithRange:NSMakeRange(0, 1)] uppercaseString]; //uppercaseString puts lowercase values with uppercase NSMutableArray *arrayForLetter = [letterDictionary objectForKey:firstLetter]; if (arrayForLetter == nil) { arrayForLetter = [NSMutableArray array]; [letterDictionary setObject:arrayForLetter forKey:firstLetter]; [sectionLetterArray addObject:firstLetter]; // This will be used to set index scroller and section titles } // Add the value to the array for this letter [arrayForLetter addObject:value]; } //Reload data in table [self.tableView reloadData]; } 

from here I do a few actions related to setting the table view after [self.tableView reloadData]; called out. The main thing is that I set the cell with string values โ€‹โ€‹to an array.

 //Display cells with data NSArray *keys = [self.letterDictionary objectForKey:[self.sectionLetterArray objectAtIndex:indexPath.section]]; NSString *key = [keys objectAtIndex:indexPath.row]; cell.textLabel.text = key; 

when the cell is then selected, the string value inside the cell is then sent back to the main view and used later as a search parameter ... The fact is that I configure several parameters that will be used as a single search string.

Looking back at the XML that I parsed

 <Rows> <Row MANUFACTURERID="76" MANUFACTURERNAME="Fondont" ISMANU="F" ISAUTO="F"/> <Row MANUFACTURERID="18" MANUFACTURERNAME="Anti" ISMANU="T" ISAUTO="T"/> </Rows> 

These are the column values โ€‹โ€‹inside the SQl table that have the MANUFACTURERID key value, which is also contained in other tables that I am analyzing. I would like to use these key values โ€‹โ€‹to limit / refine other queries, but I just canโ€™t figure out how to pass them to the parent view, where I configured all the search parameters, that is, my question is how to save a dictionary of values โ€‹โ€‹that is related to tableview selection users from the preview. Therefore, I can pass one or more of these values โ€‹โ€‹back to the subtitle of another data set to limit the information displayed depending on previous user choices.

It took me about an hour to print. Hope this makes sense, I'm still pretty new to iOS and Objective-C development, and this concept really pushes my capabilities and before I get to it and end up creating a bit of crap that I will have to fix later, I hope that one or some of you will be able to provide you with your experience in this type so that I can get it for the first time :)

If you need me to clarify anything or provide you with more information to help you help me, just let me know.

Thanks in advance!

+4
source share
3 answers

A common template for transferring information back in the view manager hierarchy is the use of delegation. You can achieve this in your scenario by doing the following:

1) Define a protocol in the SearchParametersViewController that represents your parent view controller that you mentioned.

 @protocol SearchParametersViewControllerDelegate <NSObject> @optional - (void)searchOptionsSelected:(NSArray *)selectedSearchOptions; @end 

2) Corresponds to this protocol in SearchOptionsSelectionViewController, which is a table view controller in which there is a list of selected options. Be sure to import or redirect the class defined by the protocol (for example, SearchParametersViewController).

 #import "SearchParametersViewController.h" @interface SearchOptionsSelectionViewController <SearchParametersViewControllerDelegate> 

3) Define the delegate property in your SearchOptionsSelectionViewController (assuming you are using ARC for iOS 5.0, 4.x use unsafe_unretained instead of weak . Use assign if the project is using manual memory management). This delegate object will contain a link to your parent view controller (e.g. SearchParametersViewController). You do not want this property to be saved in order to avoid storing loops / cyclic references, where one object refers to another, which in turn refers to the first and no objects are freed.

 @property (nonatomic, weak) id<SearchParametersViewControllerDelegate> delegate; 

4) When creating an instance of SearchOptionsSelectionViewController inside the parent view controller (SearchParametersViewController) set the delegate property to the instance of the parent view controller represented by the self keyword. This ensures that you can send the message (and related data) back to the hierarchy of your view controller, but the relationship of the objects remains loosely coupled. This delegate protocol could be negotiated in any other view controller, there are no hard links in the parent view presentation view controller, the only thing that connects them is the flexible adoption of the delegate protocol by the view controller.

 SearchOptionsSelectionViewController *selectionViewController = [[SearchOptionsSelectionViewController alloc] init]; selectionViewController.delegate = self; 

5) Finally, in your table table view, SearchOptionsSelectionViewController -tableView:didSelectRowAtIndexPath: transfer the data corresponding to the selected row back to the parent view controller (SearchParametersViewController) using the delegate method that you defined in the SearchParametersViewControllerDelegate protocol. You must use the -respondsToSelector: method to ensure that the delegation object actually implements the -searchOptionsSelected: delegate method. To enforce this implementation, change @optional to @required above the prototype method in the protocol definition in step # 1. self.someDataArray is the data source that you use with the select table view controller. The specifics of the delegation protocol method and the data object sent back to the parent view controller can be changed; here the delegation diagram is important and the absence of any closely related relationships between instances of any class, but especially the opposite to the view manager hierarchy.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.delegate respondsToSelector:@selector(searchOptionsSelected:)]) { NSArray *selectedObjs = [NSArray arrayWithObject:[self.someDataArray objectAtIndex:indexPath.row]]; [self.delegate searchOptionsSelected:selectedObjs] } } 

6) Implement the delegate method inside SearchOptionsSelectionViewController.m

 - (void)searchOptionsSelected:(NSArray *)selectedSearchOptions { // do what you need to with selectedSearchOptions array } 

Further reading:

Cocoa Basics Guide - Delegates and Data Sources

Cocoa Core Competencies - Protocol

+6
source

You can use the application delegate to achieve your goals here.

I'm going to assume that your application has a structure a bit like this. Please excuse the rudeness of this model.

 Application delegate (A) --> Search Options View (B) --> Table where you do selections (C) | | --> Some other view where you need the selection (D) 

Your problem is that you need information to move from C to D.

Your application delegate has the merit of universal access through the [[UIApplication sharedApplication] delegate] . Therefore, you can get a pointer to it from anywhere. With C, you can send your selection information back to A. A can either send it automatically, or D, or D can request it from A whenever you want.

A few points:

  • At the moment, I will no longer expand my answer, because now the beer is here, plus I may have misunderstood your requirement. If you need anything, I will be in childhood in the mornings in the UK, so there may be some delay.
  • Some people frowned using the application delegate as a โ€œdata dump,โ€ as I suggested. Some of these people would prefer to create an entire singleton class and treat this as a data dump. This seems to be one of those endless arguments, so I try not to interfere.
+3
source

All Articles