我需要一些帮助,试图在bandsDictionary中插入几个默认条目,以便在用户添加新条目之前它不为空。
// TableViewController.h
#import <UIKit/UIKit.h>
@class WBABand, WBABandDetailsViewController;
@interface WBABandsListTableViewController : UITableViewController
@property (nonatomic, strong) NSMutableDictionary *bandsDictionary;
@property (nonatomic, strong) NSMutableArray *firstLettersArray;
@property (nonatomic, strong) WBABandDetailsViewController *bandInfoViewController;
//NSMutableDictionary *tempDictionary;
@property (nonatomic, retain) NSMutableDictionary *tempDictionary;
-(void)addNewBand:(WBABand*)bandObject;
-(void)saveBandsDictionary;
-(void)loadBandsDictionary;
-(void)deleteBandAtIndexPath:(NSIndexPath*)indexPath;
-(void)updateBandObject:(WBABand*)bandObject atIndexPath:(NSIndexPath*)indexPath;
-(IBAction)addBandTouched:(id)sender;
@end
//TableViewController.m
#import "WBABandsListTableViewController.h"
#import "WBABand.h"
#import "WBABandDetailsViewController.h"
static NSString *bandsDictionarytKey = @"BABandsDictionarytKey";
@interface WBABandsListTableViewController ()
@end
@implementation WBABandsListTableViewController
-(id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
[self loadBandsDictionary];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.clearsSelectionOnViewWillAppear = NO;
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return self.bandsDictionary.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSString *firstLetter = [self.firstLettersArray objectAtIndex:section];
NSMutableArray *bandsForLetter = [self.bandsDictionary objectForKey:firstLetter];
return bandsForLetter.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *firstLetter = [self.firstLettersArray objectAtIndex:indexPath.section];
NSMutableArray *bandsForLetter = [self.bandsDictionary objectForKey:firstLetter];
WBABand *bandObject = [bandsForLetter objectAtIndex:indexPath.row];
// Configure the cell...
cell.textLabel.text = bandObject.name;
return cell;
}
- (void)addNewBand:(WBABand*)bandObject
{
NSString *bandNameFirstLetter = [bandObject.name substringToIndex:1];
NSMutableArray *bandsForLetter = [self.bandsDictionary objectForKey:bandNameFirstLetter];
if(!bandsForLetter)
bandsForLetter = [NSMutableArray array];
[bandsForLetter addObject:bandObject];
[bandsForLetter sortUsingSelector:@selector(compare:)];
[self.bandsDictionary setObject:bandsForLetter forKey:bandNameFirstLetter];
if(![self.firstLettersArray containsObject:bandNameFirstLetter])
{
[self.firstLettersArray addObject:bandNameFirstLetter];
[self.firstLettersArray sortUsingSelector:@selector(compare:)];
}
[self saveBandsDictionary];
}
- (void)saveBandsDictionary
{
NSData *bandsDictionaryData = [NSKeyedArchiver archivedDataWithRootObject:self.bandsDictionary];
[[NSUserDefaults standardUserDefaults] setObject:bandsDictionaryData forKey:bandsDictionarytKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)loadBandsDictionary
{
NSData *bandsDictionaryData = [[NSUserDefaults standardUserDefaults] objectForKey:bandsDictionarytKey];
if(bandsDictionaryData)
{
self.bandsDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:bandsDictionaryData];
self.firstLettersArray = [NSMutableArray arrayWithArray:self.bandsDictionary.allKeys];
[self.firstLettersArray sortUsingSelector:@selector(compare:)];
}
else
{
self.bandsDictionary = [NSMutableDictionary dictionary];
self.firstLettersArray = [NSMutableArray array];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if(self.bandInfoViewController)
{
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
if(self.bandInfoViewController.saveBand)
{
if(selectedIndexPath)
{
[self updateBandObject:self.bandInfoViewController.bandObject atIndexPath:selectedIndexPath];
[self.tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
}
else
[self addNewBand:self.bandInfoViewController.bandObject];
[self.tableView reloadData];
}
else if (selectedIndexPath)
{
[self deleteBandAtIndexPath:selectedIndexPath];
}
self.bandInfoViewController = nil;
}
}
- (IBAction)addBandTouched:(id)sender
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
self.bandInfoViewController = (WBABandDetailsViewController *)[sb instantiateViewControllerWithIdentifier:@"bandDetails"];
[self presentViewController:self.bandInfoViewController animated:YES completion:nil];
}
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.firstLettersArray objectAtIndex:section];
}
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.firstLettersArray;
}
- (int)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [self.firstLettersArray indexOfObject:title];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self deleteBandAtIndexPath:indexPath];
}
}
- (void)deleteBandAtIndexPath:(NSIndexPath*)indexPath
{
NSString *sectionHeader = [self.firstLettersArray objectAtIndex:indexPath.section];
NSMutableArray *bandsForLetter = [self.bandsDictionary objectForKey:sectionHeader];
[bandsForLetter removeObjectAtIndex:indexPath.row];
if(bandsForLetter.count == 0)
{
[self.firstLettersArray removeObject:sectionHeader];
[self.bandsDictionary removeObjectForKey:sectionHeader];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
[self.bandsDictionary setObject:bandsForLetter forKey:sectionHeader];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[self saveBandsDictionary];
}
- (void)updateBandObject:(WBABand*)bandObject atIndexPath:(NSIndexPath*)indexPath
{
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
NSString *sectionHeader = [self.firstLettersArray objectAtIndex:selectedIndexPath.section];
NSMutableArray *bandsForSection = [self.bandsDictionary objectForKey:sectionHeader];
[bandsForSection removeObjectAtIndex:indexPath.row];
[bandsForSection addObject:bandObject];
[bandsForSection sortUsingSelector:@selector(compare:)];
[self.bandsDictionary setObject:bandsForSection forKey:sectionHeader];
[self saveBandsDictionary];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
NSString *sectionHeader = [self.firstLettersArray objectAtIndex:selectedIndexPath.section];
NSMutableArray *bandsForSection = [self.bandsDictionary objectForKey:sectionHeader];
WBABand *bandObject = [bandsForSection objectAtIndex:selectedIndexPath.row];
self.bandInfoViewController = segue.destinationViewController;
self.bandInfoViewController.bandObject = bandObject;
self.bandInfoViewController.saveBand = YES;
}
@end
答案 0 :(得分:0)
您可以在loadBandsDictionary的else子句中添加一些band对象,然后保存用户默认值。你还没有提供你的WBABand
课程,所以我不知道所有的属性,但它会是这样的 -
- (void)loadBandsDictionary
{
NSData *bandsDictionaryData = [[NSUserDefaults standardUserDefaults] objectForKey:bandsDictionarytKey];
if(bandsDictionaryData)
{
self.bandsDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:bandsDictionaryData];
self.firstLettersArray = [NSMutableArray arrayWithArray:self.bandsDictionary.allKeys];
[self.firstLettersArray sortUsingSelector:@selector(compare:)];
}
else
{
self.bandsDictionary = [NSMutableDictionary dictionary];
self.firstLettersArray = [NSMutableArray array];
WBABand *sampleBand=[[WBABand alloc]init];
sampleBand.name=@"The Smashing Beatles";
//Set other band properties;
[self addNewBand:sampleBand];
WBABand *secondSampleBand=[[WBABand alloc]init]
secondSampleBand.name=@"Nine Inch Screws";
[self addNewBand:secondSampleBand];
}
}