how to save the state in userdefaults of accessory checkmark-iphone
我正在开发一个具有
状态应该被剃光,并且在下次启动应用程序时应该显示复选标记。
我已按照
我的代码:
MY.h 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | **@protocol LocationSelectionViewControllerDelegate <NSObject> @required - (void)rowSelected:(NSString *)selectedValue selectedIndex:(NSInteger)index; @end** @interface LocationSelection : UIViewController <UITableViewDelegate,UITableViewDataSource>{ UITableView *table; ***NSInteger selectedIndex;*** NSMutableArray *menuList; ***id <LocationSelectionViewControllerDelegate> delegate;*** } @property (nonatomic,retain) NSMutableArray *menuList; @property (nonatomic,retain) IBOutlet UITableView *table; ***@property (nonatomic, assign) id <LocationSelectionViewControllerDelegate> delegate;** **@property NSInteger selectedIndex;*** @end |
我的 .m 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | @implementation LocationSelection ***@synthesize menuList, table,selectedIndex,delegate;*** - (void)viewDidLoad { menuList = [[NSMutableArray alloc] initWithObjects: [NSArray arrayWithObjects:@"LOCATION1", nil], [NSArray arrayWithObjects:@"LOCATION2", nil], [NSArray arrayWithObjects:@"LOCATION3", nil], nil]; self.title = @"Location Selection"; [table reloadData]; [super viewDidLoad]; } //MY CELLFORROWATINDEXPATH - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease]; } cell.highlighted = NO; ***NSArray * rowArray = [menuList objectAtIndex:indexPath.row];*** UILabel * nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15, 8, 200, 20)] autorelease]; nameLabel.text = [NSString stringWithFormat:@"%@", [rowArray objectAtIndex:0]]; [cell.contentView addSubview:nameLabel]; ***cell.accessoryType = (rowArray == selectedIndex && selectedIndex > -1 && selectedIndex < [menuList count]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; return cell; *** } //MY DIDSELECTROWATINDEXH - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { int newRow = [indexPath row]; if (newRow != selectedIndex) { UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; newCell.accessoryType = UITableViewCellAccessoryCheckmark; if (selectedIndex > - 1 && selectedIndex < [menuList count]) { NSUInteger newIndex[] = {0, selectedIndex}; NSIndexPath *lastIndexPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2]; UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; oldCell.accessoryType = UITableViewCellAccessoryNone; } selectedIndex = newRow; NSString *selectedValue=[menuList objectAtIndex:selectedIndex]; [self.delegate rowSelected:selectedValue selectedIndex:selectedIndex]; } } |
1 2 | - (void)applicationDidEnterBackground:(UIApplication *)application { } |
使用此方法将您的数据保存到
并设置
的
在您的
当视图加载时从你的 userDefaults 加载 menuList 数组,当应用关闭时保存默认值。
在
在
您好,我认为如果您有一个对象来指示复选标记,它会适合您。您可以通过