我正在处理UItableView
的应用程序。在表的行中我能够放置checkmarks.Now如何保存复选标记的状态,以便即使用户关闭应用程序
应该削减状态,并在下一次启动应用程序时显示复选标记。
我已经按照NSUSerDefaults
上的教程进行了操作,但是把我的头发放在哪里放置保存和检索的代码。我已经尝试但是每次错误都在填充我而无法修复。
我的代码:
MY.h文件
**@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文件:
@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];
}
}
答案 0 :(得分:1)
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
使用此方法将数据保存到NSUserDefaults
并将数据设置为viewDidLoad
LocationSelection
答案 1 :(得分:0)
嗨,我认为如果你有一个指示勾选标记的对象,它会适合你。您可以通过synchronize
将其保存为用户默认值。在cellForRowATIndexPath:
中,您可以检查用户默认值中是否存在该值,如果是,则将单元格附件设为选中标记,如果不存在,则将其设置为无。
答案 2 :(得分:0)
在menuList
中放置一个反映单元格选择的状态,这样即使是多选也可以轻松完成。您可以使用字典而不是数组。数组很好但不可读,所以你必须记住哪个字段包含什么并将其映射到索引。
当视图加载来自userDefaults的menuList数组时,当应用关闭时保存默认值。
在didSelectRowAtIndexPath
中,您可以在menuList
中保存选择。
在cellForRowAtIndexPath
中,您读取menuList和新的数组索引/词典键并设置复选标记。