how is possible to make an iphone tab that loads selected data from an array and changes based on user selection.
Alx,
A simple scenario would be to create a NIB-based UITableViewCell (there are plenty of tutorials out there for this) which has a label on it of some kind.
What you can do is then grab the contents of the label when the user selects the cell and store that into a mutable array which then gets stored into the NSUserDefaults.
You can then access the NSUserDefaults from another view and use it to populate a "Favorites" tab like you've been asking about.
A little bit of sample code to help (assuming the original data is in a UITableView like you said in a prior question). I'm writing this off the top of my head (un-tested code), so you'll have to work out any bugs, but the idea is correct.
// in the .h file
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
// set up a mutable array which allows editing of the array
NSMutableArray *myFavoritesData;
}
// set up a retained property
@property (nonatomic, retain) NSMutableArray *myFavoritesData;
@end
// in the .m file
// synthesize the getters/setters for your array
@synthesize myFavoritesData;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// find cell that was just pressed
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
// get pointer for the label that we want to identify the cell by
// the tag in this case is set to '5' in Interface Builder in the options for the label
UILabel *someLabel;
someLabel = (UILabel *)[cell viewWithTag:5];
NSString *tmpFavorite = someLabel.text;
// get the count of the current array and use that for the "new" row since the count
// will always be 1 larger than the last object in the array (arrays start at 0, counts start at 1)
NSUInteger newRow = [self.myFavoritesData count];
[self.myFavoritesData insertObject:tmpFavorite atIndex:newRow];
}
// save the mutable array into NSUserDefaults when the view is about to disappear
- (void) viewWillDisappear:(BOOL)animated
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:self.myFavoritesData forKey:@"MyFavorites"];
// synchronize the data now instead of waiting for the OS to synchronize it at some
// arbitrary time in the future
[userDefaults synchronize];
}
Once you are in the new view controller, you can just read from the NSUserDefaults and populate the table from the array. For instance:
// favorites view controller
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
tmpArray = [[userDefaults objectForKey:@"MyFavorites"] mutableCopy];
if ([tmpArray count] == 0) {
//
// no favorites have ever been saved
//
} else {
// load the favorites into some array you synthesized just like before
self.tableFavoritesData = [[NSMutableArray alloc] init];
self.tableFavoritesData = [[userDefaults objectForKey:@"MyFavorites"] mutableCopy];
NSLog(@"favorites data is %d and %@", [self.tableFavoritesData count], self.tableFavoritesData);
}
[tmpArray release];
}
Then in your cellForRowAtIndexPath of the favorites view controller you just access each string in each index of the array (so, string for index 0 would go into row 0, string for index 1 would go into row 1, etc) and this is what would populate your favorites table!
Try that out.