我正在申请有UItableView
。在表格的行中,我可以放置复选标记。现在如何保存复选标记的状态,以便即使用户关闭应用程序 ,也应该删除状态并在下一次启动应用程序复选标记时显示。 我按照NSUSerDefaults
上的教程,但拉我的头发放在哪里保存和检索的代码。我已经尝试过,但每次错误填满我,无法修复。 我的代码:如何保存在userdefaults的状态附件复选标记-iphone
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];
}
}
请格式化您的代码,下次更好一点;) – 2011-04-08 07:01:48
对不起!!感谢编辑,下次再来 – Alok 2011-04-08 07:03:26