SortItemViewController.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // SortItemViewController.m
  3. // iSales-NPD
  4. //
  5. // Created by Jack on 16/9/5.
  6. // Copyright © 2016年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "SortItemViewController.h"
  9. #import "SortItemCell.h"
  10. @interface SortItemViewController ()<UITableViewDelegate,UITableViewDataSource>
  11. {
  12. CGSize tableSize;
  13. CGPoint tableOrigin;
  14. }
  15. @property (nonatomic,strong) UITableView *sortTable;
  16. @property (nonatomic,strong) UIView *tableBackgroundView;
  17. @end
  18. @implementation SortItemViewController
  19. - (instancetype)initWithTableOrigin:(CGPoint)origin {
  20. if (self = [super init]) {
  21. tableOrigin = origin;
  22. }
  23. return self;
  24. }
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. // Do any additional setup after loading the view.
  28. // tableSize = CGSizeMake(180, 200);
  29. tableSize = CGSizeZero;
  30. if (self.sortData.count) {
  31. tableSize = CGSizeMake(180, 40 * self.sortData.count);
  32. }
  33. self.view.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.5];
  34. [self.view addSubview:self.tableBackgroundView];
  35. }
  36. - (void)didReceiveMemoryWarning {
  37. [super didReceiveMemoryWarning];
  38. // Dispose of any resources that can be recreated.
  39. }
  40. #pragma mark - view
  41. - (UIView *)tableBackgroundView {
  42. if (!_tableBackgroundView) {
  43. _tableBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(tableOrigin.x, tableOrigin.y, tableSize.width, tableSize.height + 20)];
  44. _tableBackgroundView.layer.cornerRadius = 5;
  45. _tableBackgroundView.layer.masksToBounds = YES;
  46. _tableBackgroundView.backgroundColor = [UIColor whiteColor];
  47. [_tableBackgroundView addSubview:self.sortTable];
  48. }
  49. return _tableBackgroundView;
  50. }
  51. - (UITableView *)sortTable {
  52. if (!_sortTable) {
  53. _sortTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, tableSize.width, tableSize.height) style:UITableViewStylePlain];
  54. _sortTable.backgroundColor = [UIColor whiteColor];
  55. _sortTable.showsVerticalScrollIndicator = NO;
  56. _sortTable.showsHorizontalScrollIndicator = NO;
  57. _sortTable.dataSource = self;
  58. _sortTable.delegate = self;
  59. }
  60. return _sortTable;
  61. }
  62. #pragma mark data source
  63. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  64. return self.sortData.count;
  65. }
  66. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  67. SortItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  68. if (!cell) {
  69. cell = [[SortItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  70. }
  71. // cell.selectionStyle = UITableViewCellSelectionStyleNone;
  72. // switch (indexPath.row) {
  73. // case 0:{
  74. // cell.sortTitle = @"Last";
  75. // }
  76. // break;
  77. // case 1:{
  78. // cell.sortTitle = @"First";
  79. // }
  80. // break;
  81. // case 2:{
  82. // cell.sortTitle = @"Item number a-z";
  83. // }
  84. // break;
  85. // case 3:{
  86. // cell.sortTitle = @"Item number z-a";
  87. // }
  88. // break;
  89. // case 4:{
  90. // cell.sortTitle = @"Description";
  91. // }
  92. // break;
  93. //
  94. // default:
  95. // break;
  96. // }
  97. NSDictionary *sortItem = [self.sortData objectAtIndex:indexPath.row];
  98. cell.sortItem = sortItem;
  99. cell.sortIndex = indexPath.row;
  100. // if (self.sortIndex == indexPath.row) {
  101. cell.selectedSort = YES;
  102. // }
  103. return cell;
  104. }
  105. #pragma mark delegate
  106. - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  107. return 40;
  108. }
  109. - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  110. SortItemCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  111. cell.selected = NO;
  112. // dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 0.25 * NSEC_PER_SEC);
  113. // dispatch_after(time, dispatch_get_main_queue(), ^{
  114. //
  115. // });
  116. // if (indexPath.row != self.sortIndex) {
  117. // NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:self.sortIndex inSection:0];
  118. // SortItemCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
  119. // oldCell.selectedSort = NO;
  120. self.sortIndex = (int)indexPath.row;
  121. cell.selectedSort = YES;
  122. if (self.sortBlock) {
  123. self.sortBlock(self.sortIndex);
  124. }
  125. // }
  126. [self.view removeFromSuperview];
  127. }
  128. @end