2011-04-08 40 views
2

我正在申請有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]; 
    } 
} 
+0

請格式化您的代碼,下次更好一點;) – 2011-04-08 07:01:48

+0

對不起!!感謝編輯,下次再來 – Alok 2011-04-08 07:03:26

回答

0

您好我認爲它會適合你,如果你有一個對象(S)來表示對勾。您可以通過synchronize將其保存爲用戶默認值。在cellForRowATIndexPath:中,您可以檢查該值是否存在於用戶默認值中,如果是,則將該單元附件作爲選中標記,如果不存在,則將其設爲無。

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

使用此方法來將數據保存到NSUserDefaults 而在LocationSelection

0

viewDidLoad設置數據放在一個狀態在menuList反映細胞所以即使多選可以輕鬆完成的選擇。您可以使用字典而不是數組。數組很好但不可讀,所以你必須記住哪個字段包含什麼並將其映射到索引。

當視圖從你的userDefaults加載menuList數組時,當應用程序關閉時保存默認值。

didSelectRowAtIndexPath你保存選擇在menuList

cellForRowAtIndexPath您閱讀menuList和新的數組索引/字典密鑰並設置複選標記。