PickView.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #import "PickView.h"
  2. @interface PickView()<UITableViewDataSource,UITableViewDelegate>
  3. {
  4. UITableView* myTableView;
  5. /**保存那些删除时间段的按钮。数目为defState.planTimes.count;
  6. */
  7. NSMutableArray* btns;
  8. /**用于动画,不过已废弃。不好看。
  9. */
  10. UIView* centerV;
  11. UIButton* btnSt,*btnEnd;
  12. }
  13. @end
  14. @implementation PickView
  15. -(id)initWithTitle:(NSString*)tit Models:(NSArray*)mods
  16. {
  17. self = [super initWithFrame:kFrame];
  18. if (self) {
  19. _models = mods;
  20. _title = tit;
  21. [self setBackgroundColor:[UIColor colorWithWhite:.01 alpha:.3]];
  22. UIView* aView,*line;UILabel* label; UIButton* btn;
  23. CGFloat x,y,w,h,bd;
  24. bd = 10;
  25. w = kSize.width - bd*2;
  26. h = 40;
  27. x = 0;
  28. y = 0;
  29. aView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h*8 + 10)];
  30. aView.center = CGPointMake(kSize.width/2.0, kSize.height/2.0);
  31. [self addSubview:aView];
  32. [aView setBackgroundColor:[UIColor whiteColor]];
  33. centerV = aView;
  34. y = 0;
  35. label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
  36. [label setText:@"请选择时间"];
  37. if (tit) {
  38. [label setText:tit];
  39. }
  40. [aView addSubview:label];
  41. [label setTextAlignment:NSTextAlignmentCenter];
  42. y += h;
  43. line = [[UIView alloc] initWithFrame:CGRectMake(0, y, w, 3)];
  44. [line setBackgroundColor:defGreen];
  45. [aView addSubview:line];
  46. // x = bd;
  47. y += 1;
  48. h *= 6;
  49. UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(x, y, w, h)];
  50. [tv setDataSource:self];
  51. [tv setDelegate:self];
  52. [aView addSubview:tv];
  53. [tv setTableHeaderView:[UIView new]];
  54. myTableView = tv;
  55. y += h;
  56. h /=6.0;
  57. w = 200;
  58. x = (kSize.width - w)*.5;
  59. btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y+5, w, h)];
  60. [btn setTitle:@"取消" forState:UIControlStateNormal];
  61. [btn setBackgroundColor:defGreen];
  62. [btn corner:h*.5];
  63. [btn addTarget:self action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
  64. [aView addSubview:btn];
  65. }
  66. return self;
  67. }
  68. #pragma mark -
  69. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  70. {
  71. return _models.count;
  72. }
  73. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  74. {
  75. UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@""];
  76. if (!cell) {
  77. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@""];
  78. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  79. }
  80. [cell.textLabel setText:_models[indexPath.row]];
  81. return cell;
  82. }
  83. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  84. {
  85. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  86. if (clkBlock) {
  87. NSString* str = [NSString stringWithFormat:@"%d",(int)indexPath.row];
  88. clkBlock(str);
  89. }
  90. [self removeFromSuperview];
  91. }
  92. -(void)completion:(MyBlockType)comp
  93. {
  94. clkBlock = comp;
  95. }
  96. -(void)show
  97. {
  98. [[UIApplication sharedApplication].keyWindow addSubview:self];
  99. }
  100. @end