Reputation: 591
I am trying to get started with NSMutableArrays. I have a UITableView that displays a list of items. I would like to store certain variables (NSStrings) in an array as a row is scrolled past. I am able to do this with NSLog, but am having trouble setting up the NSMutableArray. This is how I currently have it setup.
In the cellForRowAtIndexPath
method of my .m file, I am using the following NSStrings:
I would like to store these in an NSMutableArray called scrolledOver. I would like to use the terms "key1, key2, key3..." to identify the elements of the array.
In my .h file, I am declaring the NSMutable array like this:
@Interface viewController {
NSMutableArray *scrolledOver;
}
@property (nonatomic, retain) NSMutableArray *scrolledOver;
As I scroll over each row I just want to append the new strings to the array... Any ideas would be very helpful! Thank you!
Upvotes: 0
Views: 105
Reputation: 9690
Don't add these values to a dictionary. It's unnecessary and if one of your keys changes you wont know about it.
Instead, make an NSObject subclass with custom properties. This will give you hard errors if a variable name or something changes in the future, making it impossible for you to accidentally forget to change a key in all areas of your app.
I'm assuming these variables define a "place", so something like the following:
@interface Place : NSObject
@property (nonatomic, assign) NSUInteger placeId;
@property (nonatomic, strong) NSDate* currentTime;
@property (nonatomic, copy) NSString* name;
@property (nonatomic, assign) double latitude;
@property (nonatomic, assign) double longitude;
@end
.
@implementation Place
@synthesize placeId;
@synthesize currentTime;
@synthesize name;
@synthesize latitude;
@synthesize longitude;
@end
Now initialize your places and add them to an array that will be used as your datasource. Then when you setup your cell, pull out the place for that indexPath and adjust the cell.
Implement -tableView:willDisplayCell:forRowAtIndexPath: and use this to add them into your scrolledOver array. To make sure they're unique, change it to an NSMutableSet or NSMutableDictionary
Upvotes: 0
Reputation: 16124
You can create a dictionary for that cell and add it to the array as follows:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:placeId forKey:@"key1"];
[dict setObject:CurrentTime forKey:@"key2"];
....
[scrolledOver addObject:dict];
Upvotes: 1