问题描述
解决方案
下面的是我的代码
//设置商品分类的子分类
_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
解决方案二:
这是先添加下拉,再加3,下拉就在3下面了,试试点击后添加到view,选择后隐藏