PasswordSaveViewController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // PasswordSaveViewController.m
  3. // FunSDKDemo
  4. //
  5. // Created by wujiangbo on 2018/11/2.
  6. // Copyright © 2018年 wujiangbo. All rights reserved.
  7. //
  8. #import "PasswordSaveViewController.h"
  9. #import "UserInfoCell.h"
  10. #import "Header.h"
  11. @interface PasswordSaveViewController ()<UITableViewDelegate,UITableViewDataSource>
  12. @property (nonatomic, strong) UITableView *mainTableView; //账号密码列表
  13. @property (nonatomic, strong) NSMutableDictionary *dataDic;//数据字典
  14. @end
  15. @implementation PasswordSaveViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. self.view.backgroundColor = [UIColor whiteColor];
  19. [self.view addSubview:self.mainTableView];
  20. //设置导航栏
  21. [self configNav];
  22. //获取本地用户名和密码数据
  23. [self getLocalData];
  24. }
  25. #pragma mark - 设置导航栏
  26. -(void)configNav
  27. {
  28. self.navigationItem.title = TS("user_psw");
  29. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"new_back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backItemClicked)];
  30. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  31. self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
  32. UIBarButtonItem *cleatBtn = [[UIBarButtonItem alloc] initWithTitle:TS("delete") style:UIBarButtonItemStyleDone target:self action:@selector(cleatBtnClicked)];
  33. self.navigationItem.rightBarButtonItem = cleatBtn;
  34. cleatBtn.tintColor = [UIColor whiteColor];
  35. self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  36. }
  37. #pragma mark - 获取本地用户名和密码数据
  38. -(void)getLocalData
  39. {
  40. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  41. self.dataDic = [[defaults objectForKey:@"DemoUserInfo"] mutableCopy];
  42. }
  43. #pragma mark - EventAction
  44. #pragma mark 返回上一层
  45. -(void)backItemClicked
  46. {
  47. [self.navigationController popViewControllerAnimated:YES];
  48. }
  49. #pragma mark 清空所有数据
  50. -(void)cleatBtnClicked
  51. {
  52. [self.dataDic removeAllObjects];
  53. [self.mainTableView reloadData];
  54. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  55. [defaults setObject:self.dataDic forKey:@"DemoUserInfo"];
  56. }
  57. #pragma mark 删除某一行数据,并刷新列表
  58. -(void)deleteData:(NSString *)userName
  59. {
  60. [self.dataDic removeObjectForKey:userName];
  61. [self.mainTableView reloadData];
  62. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  63. [defaults setObject:self.dataDic forKey:@"DemoUserInfo"];
  64. }
  65. #pragma mark - tableViewDataSource/Delegate
  66. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  67. return self.dataDic.allKeys.count;
  68. }
  69. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  70. return 50;
  71. }
  72. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  73. UserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserInfoCell"];
  74. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  75. NSString *useName = [self.dataDic.allKeys objectAtIndex:indexPath.row];
  76. NSString *passWord = [self.dataDic objectForKey:useName];
  77. cell.mainTitle.text = useName;
  78. cell.descriptionTitle.text = passWord;
  79. return cell;
  80. }
  81. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  82. {
  83. return true;
  84. }
  85. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. if (editingStyle == UITableViewCellEditingStyleDelete) {
  88. [self deleteData:[self.dataDic.allKeys objectAtIndex:indexPath.row]];
  89. }
  90. }
  91. #pragma mark - lazyLoad 懒加载
  92. - (UITableView *)mainTableView {
  93. if (!_mainTableView) {
  94. _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 0, ScreenWidth - 20, ScreenHeight) style:UITableViewStylePlain];
  95. _mainTableView.delegate = self;
  96. _mainTableView.dataSource = self;
  97. _mainTableView.estimatedRowHeight = 0;
  98. [_mainTableView registerClass:[UserInfoCell class] forCellReuseIdentifier:@"UserInfoCell"];
  99. _mainTableView.tableFooterView = [UIView new];
  100. }
  101. return _mainTableView;
  102. }
  103. -(NSMutableDictionary *)dataDic
  104. {
  105. if (!_dataDic) {
  106. _dataDic = [[NSMutableDictionary alloc] initWithCapacity:0];
  107. }
  108. return _dataDic;
  109. }
  110. @end