2016-09-27 56 views
1

我使用下面的代碼顯示在UITableView的如何保存uitableview中的複選標記並在用戶再次返回到視圖時顯示它們?

{ 
    // NSArray *tableContents; 
    NSMutableArray *selectedMarks; // You need probably to save the selected cells for use in the future. 
} 
@property (strong, nonatomic) IBOutlet UITableView *languageTableView; 
@property (nonatomic, strong) NSArray *tableContents; 

@end 

@implementation QPLanguageSettingsController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    [self initialisation]; 
    selectedMarks = [NSMutableArray new]; 
} 
#pragma mark - View Life Cycle 

-(void)initialisation 
{ 
    _tableContents = [NSArray arrayWithObjects:@"English",@"Spanish",@"Russian",@"Arabic",@"Portuguese",@"French",@"German",@"German",@"German",@"German",@"German",@"German",@"German",@"German", nil]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - UITableView delegate & datasources 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 14; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"newFriendCell"; 
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    //etc. 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    cell.backgroundColor = [UIColor clearColor]; 
    [cell setIndentationLevel:3]; 
    [cell setIndentationWidth:10]; 
    NSString *text = [_tableContents objectAtIndex:[indexPath row]]; 
    //cell.isSelected = [selectedMarks containsObject:text] ? YES : NO; 
    cell.textLabel.text = text; 
    NSDictionary *item = [_tableContents objectAtIndex:indexPath.row]; 
    if ([selectedMarks containsObject:item]) 
    { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else 
    { 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    //if you want only one cell to be selected use a local NSIndexPath property instead of array. and use the code below 
    //self.selectedIndexPath = indexPath; 

    //the below code will allow multiple selection 
    NSDictionary *item = [_tableContents objectAtIndex:indexPath.row]; 
    if ([selectedMarks containsObject:item]) 
    { 
     [selectedMarks removeObject:item]; 
    } 
    else 
    { 
     [selectedMarks addObject:item]; 
    } 
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

的對號但問題是,當我再次來到視圖控制器的所有複選標記消失。如何解決它。記得我在uitableview中使用了多個選擇。

+0

存儲數據或Plist或用戶默認值。 –

+0

如何將此存儲在nsuserdefaults中,請發佈代碼 –

+0

保存您在數組中檢查標記,然後使用userdefaults保存數組''[NSUserDefaults standardUserDefaults] setObject:yourdataAry forKey:@「checkMarks」];或使用本地數據庫。 – vaibhav

回答

3

我試着爲你的問題得到解決方案。我成功了。它完美的工作。

這是示例one.Try this code.It works fine。

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> 

@property (strong, nonatomic) IBOutlet UITableView *tableViewCheckMarkPreviousSelectionUpdate; 
@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 
{ 
    NSMutableArray *arrProductSelection,*arrProductSelectDeSelectCheckMark; 
    NSArray *arrayFetchFromDefaults; 
} 

@end 

@implementation ViewController 

@synthesize tableViewCheckMarkPreviousSelectionUpdate; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    arrProductSelection = [[NSMutableArray alloc]initWithObjects:@"iPhone",@"iPad",@"iPod",@"iTV",@"iWatch",@"iMac",nil]; 
} 
- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(void)viewWillAppear:(BOOL)animated 
{ 
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
    arrayFetchFromDefaults = [userDefaults objectForKey:@"selectedcheckmark"]; 
    arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]initWithArray:arrayFetchFromDefaults]; 
    if(arrProductSelectDeSelectCheckMark.count == 0) 
    { 
    arrProductSelectDeSelectCheckMark = [[NSMutableArray alloc]init]; 
    for(int j=0;j<[arrProductSelection count];j++) 
    { 
     [arrProductSelectDeSelectCheckMark addObject:@"deselected"]; 
    } 
    } 
    [tableViewCheckMarkPreviousSelectionUpdate reloadData]; 
} 

#pragma mark - UITableViewDataSource Methods 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return arrProductSelection.count; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *strCell = @"cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell]; 
    if(cell==nil) 
    { 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell]; 
    } 
    if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"]) 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    else 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    cell.textLabel.text = [arrProductSelection objectAtIndex:indexPath.row]; 
    return cell; 
} 

#pragma mark - UITableViewDelegate Methods 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    @try 
    { 
     CGPoint touchPoint = [cell convertPoint:CGPointZero toView:tableViewCheckMarkSelectionUpdate]; 
     NSIndexPath *indexPath = [tableViewCheckMarkSelectionUpdate indexPathForRowAtPoint:touchPoint]; 
     NSLog(@"%@",arrProductSelectDeSelectCheckMark); 
     if([arrProductSelectDeSelectCheckMark count]==0) 
     { 
      for(int i=0; i<[arrProductSelection count]; i++) 
      { 
      [arrProductSelectDeSelectCheckMark addObject:@"deselected"]; 
      } 
     } 
     if([[arrProductSelectDeSelectCheckMark objectAtIndex:indexPath.row] isEqualToString:@"deselected"]) 
     { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      [arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"selected"]; 
     } 
     else 
     { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
      [arrProductSelectDeSelectCheckMark replaceObjectAtIndex:indexPath.row withObject:@"deselected"]; 
     } 

     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
     [defaults setObject:arrProductSelectDeSelectCheckMark forKey:@"selectedcheckmark"]; 
     [defaults synchronize]; 
     } 
    @catch (NSException *exception) { 
    NSLog(@"The exception is-%@",exception); 
    } 
} 
@end 
+0

謝謝....有什麼辦法可以獲得數組中選定的項目? –

+0

在arrProductSelectDeSelectCheckMark數組中,我們可以選擇並取消選擇項目。 – user3182143

0

你有多個解決方案:

  1. Core-dataSQLite這實際上符合你的使用情況,但3號是更適合您的使用情況下,設備上使用合理的DB。
  2. 使用NSUserDefaults

在運行時,您可以使用NSUserDefaults對象讀取您的應用程序從用戶的默認數據庫使用的默認值。 NSUserDefaults緩存信息,以避免每次需要默認打開用戶的默認數據庫時

您可以找到一個示例here。但NSUserDefaults是保存設置/配置/設置信息或用戶信息,而不是您需要的用例。

  1. 使用NSCoder這在我看來符合您的使用案例。
+0

請告訴我如何使用nsuserdefaults –

+0

您是否閱讀過答案?另外,你應該避免在NSUserDefaults中保存你需要的東西。你需要的是NSCoder。 – OhadM

0

可以在其上建立一個獨立的,並存儲數據:

Singleton.h 

@interface Singleton : NSObject 

@property (nonatomic, strong) NSArray *rowsChecked; 

+ (Singleton *)sharedInstance; 
+ (NSArray *)getRowsChecked; 
+ (void)setRowsChecked:(NSArray *)rowsChecked; 

@end 

Singleton.m

#import "Singleton.h" 

@implementation Singleton 

     static Singleton *sharedObject; 

     + (Singleton *)sharedInstance; 
     { 
      if (sharedObject == nil) { 
       static dispatch_once_t pred; 
       dispatch_once(&pred, ^{ 
        sharedObject = [[Singleton alloc] init]; 
       }); 
      } 
      return sharedObject; 
     } 

     + (NSArray *)getRowsChecked 
     { 
      Singleton *singleton = [Singleton sharedInstance]; 
      return singleton.rowsChecked; 
     } 

     + (void)setRowsChecked:(NSArray *)rowsChecked 
     { 
      Singleton *singleton = [Singleton sharedInstance]; 
      singleton.rowsChecked= rowsChecked; 
     } 

    @end 

,並訪問您的辛格爾頓:

[[Singleton sharedInstance] getRowsChecked] 

// or 

[[Singleton sharedInstance] setRowsChecked:anArray]; 
+0

很酷,那麼他是如何插入並通過遵循你的ans得到檢查的數組? – vaibhav

+0

訪問getRowsChecked並插入setRowsChecked –

+0

那麼一次可以保存多少個實例..如果多個複選標記數組需要保存,該怎麼辦? – vaibhav

0

使用下面的代碼:

#import "ViewController.h" 

@interface ViewController()<UITableViewDelegate, UITableViewDataSource> 
@property (weak, nonatomic) IBOutlet UITableView *languagesTableView; 

@property (strong, nonatomic) NSArray *languagesArray; 

@property (strong, nonatomic) NSMutableArray *checkMarksArray; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    [self.languagesTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; 
    self.languagesArray = [NSArray arrayWithObjects:@"English",@"Spanish",@"Russian",@"Arabic",@"Portuguese",@"French",@"German", nil]; 
    self.checkMarksArray = [[NSMutableArray alloc]init]; 
    if([[NSUserDefaults standardUserDefaults]objectForKey:@"selectedRowsArray"]) 
    { 
     self.checkMarksArray = [[[NSUserDefaults standardUserDefaults]objectForKey:@"selectedRowsArray"] mutableCopy]; 
    } 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return self.languagesArray.count; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *languagesCell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 
    languagesCell.textLabel.text = self.languagesArray[indexPath.row]; 
    if([self.checkMarksArray containsObject:[NSNumber numberWithLong:indexPath.row]]) 
    { 
     languagesCell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } 
    else 
    { 
     languagesCell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    [[NSUserDefaults standardUserDefaults]setObject:self.checkMarksArray forKey:@"selectedRowsArray"]; 
    [[NSUserDefaults standardUserDefaults]synchronize]; 

    return languagesCell; 


} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 

    if([self.checkMarksArray containsObject:[NSNumber numberWithLong:indexPath.row]]) 
    { 
     [self.checkMarksArray removeObject:[NSNumber numberWithLong:indexPath.row]]; 
    } 
    else 
    { 
     [self.checkMarksArray addObject:[NSNumber numberWithLong:indexPath.row]]; 
    } 

    [self.languagesTableView reloadData]; 
} 

@end 

入住此GitHub的鏈接:在覈心數據或sqlite的

https://github.com/k-sathireddy/TableViewSelectedCheckMarks

+0

我檢查了...有沒有什麼辦法讓逗號附加選定的行值? –

+0

你想將數組轉換爲逗號分隔的字符串? – KSR

相關問題