2011-04-08 6 views
2

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]; 
    } 
} 
+0

다음 번에 코드를 좀 더 포맷하십시오.) –

+0

죄송합니다! 다음번에 편집 해 주셔서 감사합니다. – Alok

답변

0

안녕하세요 난 당신이 객체 (들)이 있다면 그것은 당신을 맞게됩니다 생각은 체크 표시를 나타냅니다 . 이것을 synchronize을 통해 사용자 디폴트 값으로 저장할 수 있습니다. cellForRowATIndexPath:에서 사용자 기본값에 값이 있는지 확인할 수 있습니다. 그렇다면 셀 액세서리를 체크 표시로 만들고 존재하지 않으면 셀 액세서리를 없음으로 만듭니다.

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

이 방법을 사용 너무도 다중 선택을 쉽게 할 수있는 셀의 선택을 반영하여 menuList 상태를 넣어 NSUserDefaults 에 데이터를 저장하고 LocationSelection

0

viewDidLoad의 데이터를 설정합니다. 배열 대신 사전을 사용할 수 있습니다. 배열은 괜찮지 만 읽을 수는 없으므로 어떤 필드에 무엇이 들어 있는지 기억하고 색인에 매핑해야합니다.

뷰가로드되어 userDefaults에서 menuList 배열을로드하면 앱이 닫히면 기본값이 저장됩니다.

didSelectRowAtIndexPath에서 선택 사항은 menuList에 저장됩니다.

cellForRowAtIndexPath에서 menuList와 새 배열 색인/사전 키를 읽고 체크 표시를 설정합니다.

관련 문제