OptionPopOverViewController.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // OptionPopOverViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 4/20/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "OptionPopOverViewController.h"
  9. #import "CustomLabel.h"
  10. #import "CustomImageView.h"
  11. #import "WYPopoverController.h"
  12. #define kfOptionPopOverWidth 180.0f
  13. #define kfOptionPopOverTableViewCellHeight 45.0f
  14. /*
  15. @implementation OptionPopOverData
  16. - (instancetype)initWithDictionary:(NSDictionary *)dic {
  17. if (self = [super init]) {
  18. self.iconName = dic[@"iconName"];
  19. self.menuName = dic[@"menuName"];
  20. self.target = dic[@"target"];
  21. self.selector = [dic[@"selector"] pointerValue];
  22. }
  23. return self;
  24. }
  25. @end
  26. */
  27. @implementation OptionPopOverTableViewCell
  28. - (void)awakeFromNib {
  29. self.selectionStyle = UITableViewCellSelectionStyleNone;
  30. }
  31. @end
  32. @interface OptionPopOverViewController () <UITableViewDataSource, UITableViewDelegate> {
  33. }
  34. @end
  35. #pragma mark - Class Definition
  36. @implementation OptionPopOverViewController
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. // Do any additional setup after loading the view.
  40. [self initUI];
  41. [self prepareViewDidLoad];
  42. }
  43. - (void)initUI {
  44. CGFloat height = kfOptionPopOverTableViewCellHeight * self.dataArray.count;
  45. self.preferredContentSize = CGSizeMake(180, height);
  46. _tableView.dataSource = self;
  47. _tableView.delegate = self;
  48. _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  49. _tableView.separatorColor = kUILineColor2;
  50. _tableView.backgroundColor = [UIColor clearColor];
  51. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  52. _tableView.scrollEnabled = NO;
  53. _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
  54. }
  55. - (void)prepareViewDidLoad {
  56. }
  57. - (void)viewDidAppear:(BOOL)animated {
  58. [super viewDidAppear:animated];
  59. [_tableView reloadData];
  60. }
  61. #pragma mark - Main Logic
  62. #pragma mark - UITableView DataSource & Delegate
  63. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  64. return 1;
  65. }
  66. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  67. return _dataArray.count;
  68. }
  69. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  70. static NSString *CellIdentifier = @"CellIdentifier";
  71. OptionPopOverTableViewCell *cell = (OptionPopOverTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  72. if (cell == nil) {
  73. cell = [[OptionPopOverTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  74. }
  75. NSDictionary *option = _dataArray[indexPath.row];
  76. cell.lblMenuName.text = option[@"menuName"];
  77. cell.icon.image = [UIImage imageNamed:option[@"iconName"]];
  78. return cell;
  79. }
  80. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  81. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  82. NSDictionary *option = _dataArray[indexPath.row];
  83. id target = option[@"target"];
  84. SEL selector = [option[@"selector"] pointerValue];
  85. [_poc dismissPopoverAnimated:YES completion:^{
  86. [target performSelector:selector withObject:nil afterDelay:0.0f];
  87. }];
  88. }
  89. - (void)popoverControllerDidDismissPopover:(WYPopoverController *)controller
  90. {
  91. _poc.delegate = nil;
  92. _poc = nil;
  93. }
  94. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  95. // Remove seperator inset
  96. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  97. [cell setSeparatorInset:UIEdgeInsetsZero];
  98. }
  99. // Prevent the cell from inheriting the Table View's margin settings
  100. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  101. [cell setPreservesSuperviewLayoutMargins:NO];
  102. }
  103. // Explictly set your cell's layout margins
  104. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  105. [cell setLayoutMargins:UIEdgeInsetsZero];
  106. }
  107. }
  108. #pragma mark - UI Events
  109. #pragma mark - MemoryWarning
  110. - (void)didReceiveMemoryWarning
  111. {
  112. [super didReceiveMemoryWarning];
  113. // Dispose of any resources that can be recreated.
  114. }
  115. @end