我正在申請有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