奔跑的香蕉Go 2015-11-04 08:53 采纳率: 55.6%
浏览 2202

请问下我的这个下拉菜单,一拉出来就被别的覆盖掉了,怎么办?

图片说明

我想让他点出来的时候在最上面

  • 写回答

2条回答 默认 最新

  • 奔跑的香蕉Go 2015-11-04 09:24
    关注

    下面的是我的代码
    //设置商品分类的子分类
    _comboBoxClass = [[ComboBoxView alloc] initWithFrame:CGRectMake(180, 120, 100 , 130)];
    _comboBoxClass.comboBoxDatasource = comboBoxDatasource;
    _comboBoxClass.backgroundColor = [UIColor clearColor];
    [ _comboBoxClass setContent:[comboBoxDatasource objectAtIndex:0]];
    [self.view addSubview: _comboBoxClass];

    • (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { [self initVariables]; [self initCompentWithFrame:frame]; } return self; }

    #pragma mark -
    #pragma mark custom methods

    • (void)initVariables {
      _showComboBox = NO;
      }

    • (void)initCompentWithFrame:(CGRect)frame {
      _selectContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 60, 25)];
      _selectContentLabel.font = [UIFont systemFontOfSize:14.0f];
      _selectContentLabel.backgroundColor = [UIColor clearColor];
      [self addSubview:_selectContentLabel];

      _pulldownButton = [UIButton buttonWithType:UIButtonTypeCustom];
      [_pulldownButton setFrame:CGRectMake(frame.size.width - 25, 0, 25, 25)];
      [_pulldownButton setBackgroundImage:[UIImage imageNamed:@"list_ico_d"] forState:UIControlStateNormal];
      [_pulldownButton addTarget:self action:@selector(pulldownButtonWasClicked:) forControlEvents:UIControlEventTouchUpInside];
      [self addSubview:_pulldownButton];

      _hiddenButton = [UIButton buttonWithType:UIButtonTypeCustom];
      [_hiddenButton setFrame:CGRectMake(0, 0, frame.size.width - 25, 25)];
      _hiddenButton.backgroundColor = [UIColor clearColor];
      [_hiddenButton addTarget:self action:@selector(pulldownButtonWasClicked:) forControlEvents:UIControlEventTouchUpInside];
      [self addSubview:_hiddenButton];

      _comboBoxTableView = [[UITableView alloc] initWithFrame:CGRectMake(1, 26, frame.size.width -2, frame.size.height - 27)];
      _comboBoxTableView.dataSource = self;
      _comboBoxTableView.delegate = self;
      _comboBoxTableView.backgroundColor = [UIColor clearColor];
      _comboBoxTableView.separatorColor = [UIColor blackColor];
      _comboBoxTableView.hidden = YES;
      [self addSubview:_comboBoxTableView];

    }

    • (void)setContent:(NSString *)content {
      _selectContentLabel.text = content;
      }

    • (void)show {
      _comboBoxTableView.hidden = NO;
      _showComboBox = YES;
      [self setNeedsDisplay];
      }

    • (void)hidden {
      _comboBoxTableView.hidden = YES;
      _showComboBox = NO;
      [self setNeedsDisplay];
      }

    #pragma mark -
    #pragma mark custom event methods

    • (void)pulldownButtonWasClicked:(id)sender { if (_showComboBox == YES) { [self hidden]; }else { [self show]; } }

    #pragma mark -
    #pragma mark UITableViewDelegate and UITableViewDatasource methods

    • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      return [_comboBoxDatasource count];
      }

    • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      static NSString *CellIdentifier = @"ListCellIdentifier";
      UITableViewCell *cell = [_comboBoxTableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; }
      cell.textLabel.text = (NSString *)[_comboBoxDatasource objectAtIndex:indexPath.row];
      cell.textLabel.font = [UIFont systemFontOfSize:13.0f];
      cell.accessoryType = UITableViewCellAccessoryNone;
      cell.selectionStyle = UITableViewCellSelectionStyleNone;

      return cell;
      }
      //设置tableview的数据靠左
      -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
      {
      if ([cell respondsToSelector:@selector(setSeparatorInset:)])
      { [cell setSeparatorInset:UIEdgeInsetsZero]; }
      if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
      { [cell setPreservesSuperviewLayoutMargins:NO]; }
      if ([cell respondsToSelector:@selector(setLayoutMargins:)])
      { [cell setLayoutMargins:UIEdgeInsetsZero]; }}

    • (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
      return 25.0f;
      }

    • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      [self hidden];
      _selectContentLabel.text = (NSString *)[_comboBoxDatasource objectAtIndex:indexPath.row];
      }

    • (void)drawListFrameWithFrame:(CGRect)frame withContext:(CGContextRef)context {
      CGContextSetLineWidth(context, 1.0f);
      CGContextSetRGBStrokeColor(context, 0.0f, 0.0f, 0.0f, 1.0f);
      if (_showComboBox == YES) {
      CGContextAddRect(context, CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height));

      }else {
      CGContextAddRect(context, CGRectMake(0.0f, 0.0f, frame.size.width, 25.0f));
      }
      CGContextDrawPath(context, kCGPathStroke);
      CGContextMoveToPoint(context, 0.0f, 25.0f);
      CGContextAddLineToPoint(context, frame.size.width, 25.0f);
      CGContextMoveToPoint(context, frame.size.width - 25, 0);
      CGContextAddLineToPoint(context, frame.size.width - 25, 25.0f);

      CGContextStrokePath(context);
      }

    #pragma mark -
    #pragma mark drawRect methods

    • (void)drawRect:(CGRect)rect { [self drawListFrameWithFrame:self.frame withContext:UIGraphicsGetCurrentContext()]; }

    #pragma mark -
    #pragma mark dealloc memery methods

    @end

    评论

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了