SortItemViewController.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. self.view.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.5];
  30. [self.view addSubview:self.tableBackgroundView];
  31. }
  32. - (void)didReceiveMemoryWarning {
  33. [super didReceiveMemoryWarning];
  34. // Dispose of any resources that can be recreated.
  35. }
  36. #pragma mark - view
  37. - (UIView *)tableBackgroundView {
  38. if (!_tableBackgroundView) {
  39. _tableBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(tableOrigin.x, tableOrigin.y, tableSize.width, tableSize.height + 20)];
  40. _tableBackgroundView.layer.cornerRadius = 5;
  41. _tableBackgroundView.layer.masksToBounds = YES;
  42. _tableBackgroundView.backgroundColor = [UIColor whiteColor];
  43. [_tableBackgroundView addSubview:self.sortTable];
  44. }
  45. return _tableBackgroundView;
  46. }
  47. - (UITableView *)sortTable {
  48. if (!_sortTable) {
  49. _sortTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, tableSize.width, tableSize.height) style:UITableViewStylePlain];
  50. _sortTable.backgroundColor = [UIColor whiteColor];
  51. _sortTable.showsVerticalScrollIndicator = NO;
  52. _sortTable.showsHorizontalScrollIndicator = NO;
  53. _sortTable.dataSource = self;
  54. _sortTable.delegate = self;
  55. }
  56. return _sortTable;
  57. }
  58. #pragma mark data source
  59. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  60. return 5;
  61. }
  62. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  63. SortItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  64. if (!cell) {
  65. cell = [[SortItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  66. }
  67. // cell.selectionStyle = UITableViewCellSelectionStyleNone;
  68. switch (indexPath.row) {
  69. case 0:{
  70. cell.sortTitle = @"Last";
  71. }
  72. break;
  73. case 1:{
  74. cell.sortTitle = @"First";
  75. }
  76. break;
  77. case 2:{
  78. cell.sortTitle = @"Item number a-z";
  79. }
  80. break;
  81. case 3:{
  82. cell.sortTitle = @"Item number z-a";
  83. }
  84. break;
  85. case 4:{
  86. cell.sortTitle = @"Description";
  87. }
  88. break;
  89. default:
  90. break;
  91. }
  92. cell.sortIndex = indexPath.row;
  93. // if (self.sortIndex == indexPath.row) {
  94. cell.selectedSort = YES;
  95. // }
  96. return cell;
  97. }
  98. #pragma mark delegate
  99. - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  100. return 40;
  101. }
  102. - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  103. SortItemCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  104. cell.selected = NO;
  105. // dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 0.25 * NSEC_PER_SEC);
  106. // dispatch_after(time, dispatch_get_main_queue(), ^{
  107. //
  108. // });
  109. // if (indexPath.row != self.sortIndex) {
  110. // NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:self.sortIndex inSection:0];
  111. // SortItemCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
  112. // oldCell.selectedSort = NO;
  113. self.sortIndex = indexPath.row;
  114. cell.selectedSort = YES;
  115. if (self.sortBlock) {
  116. self.sortBlock(self.sortIndex);
  117. }
  118. // }
  119. [self.view removeFromSuperview];
  120. }
  121. @end