views:

18

answers:

1

I have a mapView and tableView, with each annotation in the mapView corresponding with a cell in the table. What I want to do is select the appropriate cell anytime an annotation is selected on the map.

As of right now, im creating an NSDictionary when the cells are created, which maps the row number to the annotationID. This works, but the problem is that the dictionary isnt completely populated until all the cells have been created, and all the cells arent created until youve scrolled all the way through the table. Thus, when the app starts for the first time, only the 4 annotations originally visible can be selected from the mapView.

So what im looking for is either a method to automatically populate my dictionary, or a better way of accomplishing what i need to do. Thanks a lot!

+1  A: 

Well, first thoughts are that instead of populating the NSDictionary during scrolling, just populate it during viewDidLoad ..I do something similar where I populate all of my data into an NSDictionary and then use that to initialize/update the UI on the cells during scrolling for re-usable cells.

Also, by putting your data into an array of some kind, you can also map it to the cells. Just remember that arrays start at 0, so position in table = indexOfYourArray + 1

Simple example of loading an array stored in a plist into an NSArray in viewDidLoad

// Load the data 
    NSString *pathToFile = [[NSBundle mainBundle] pathForResource:someArrayNameString ofType:@"plist"];
    self.someArrayYouCreated = [NSArray arrayWithContentsOfFile:pathToFile];

Now that you just dumped a whole bunch of data into that array, you can populate it during scrolling in cellForRowAtIndexPath

NSDictionary *dataItem = [someArrayYouCreated objectAtIndex:indexPath.row];

UILabel *label;
label = (UILabel *)[cell viewWithTag:1];
label.textColor = [UIColor colorWithRed:(58/255.f) green:(58/255.f) blue:(58/255.f) alpha:1.0];
label.text = [dataItem objectForKey:@"PersonName"];

With this example, you are just populating the cells from an array that you created in viewDidLoad and therefore all of your data is ready to use almost right away. If you have a ton of data, you could also throw up a progress circle to delay for a second until the array is finished loading.

Again, I don't quite understand how you are storing your data (since you have not said anything about that) so I can only speculate that this will work for you. Until you provide more detail, this is the best I can do.

iWasRobbed
Im not sure i can do this for two reasons:1) I'm not ready to populate the table until all the data is loaded.2) The data is sorted in the cellForRowAtIndexPath: method, and im having trouble imagining how id do it elsewhere.Thanks
culov
1) The data loads into an `NSDictionary` first and then as the user scrolls `cellForRowAtIndexPath` populates each cell by pulling data from the `NSDictionary`...think of the `NSDictionary` as a repository for your data. 2) I don't know how/where you are retrieving or storing your data since you still haven't elaborated on that, but there are many ways to sort it before you populate it. You can initialize one array and then dump the sorted results of that array into a new array which you use to populate the table.
iWasRobbed
See updated answer above for an example.
iWasRobbed
This is almost exactly what I did. Thanks.
culov
Glad to hear it! Best of luck
iWasRobbed