UItableView
을 갖는 응용 프로그램에서 작업하고 있습니다. 테이블의 행에서 체크 표시를 넣을 수 있습니다. 이제 체크 표시의 상태를 저장하여 사용자가 애플리케이션을 닫을지라도 상태를 면도해야하고 애플리케이션의 다음 실행에 체크 표시해야합니다. 나는 NSUSerDefaults
에 관한 튜토리얼을 따라 갔지만, 내 머리카락을 당겨서 저장하고 가져 오는 코드를 어디에 둘 것인지를 시도했다. 그러나 오류가있을 때마다 나를 괴롭 히고 수정할 수 없다. 내 코드 :액세서리 checkmark-iphone의 userdefaults에 상태를 저장하는 방법
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];
}
}
다음 번에 코드를 좀 더 포맷하십시오.) –
죄송합니다! 다음번에 편집 해 주셔서 감사합니다. – Alok