gpt4 book ai didi

iphone - 如何在配件复选标记-iPhone 的用户默认值中保存状态

转载 作者:行者123 更新时间:2023-12-03 21:20:09 25 4
gpt4 key购买 nike

我正在开发一个具有UItableView的应用程序。在表的行中,我可以放置复选标记。现在如何保存复选标记的状态,以便即使用户关闭应用程序该状态应该被删除,并且在下次启动应用程序时应该显示复选标记。我已经按照 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];
}
}

最佳答案

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

使用此方法将数据保存到 NSUserDefaults并在LocationSelection

viewDidLoad中设置数据

关于iphone - 如何在配件复选标记-iPhone 的用户默认值中保存状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5591469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com