我有这个自定义视图 - 它是一个DropDown视图,允许我在功能上下拉。
这是它的.m文件:
@implementation DropDownView
/**
The state of the drop down
*/
BOOL isOpen = false;
/**
The current z position
*/
float currentZPoisition;
/**
The current table z position
*/
float currentTableZPoisition;
/**
The drop down view
*/
UIView* dropDown;
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self)
{
dropDown = [[[NSBundle mainBundle] loadNibNamed:VIEW_DROP_DOWN
owner:self
options:nil] objectAtIndex:0];
[self setView];
}
return self;
}
-(void)setDropDownData:(NSArray*)data {
if (!data || data.count < 1)
return;
_title.text = data[0];
_currentValue = [NSNumber numberWithInt:0];
_dropDownOptions = data;
[_optionsTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
}
/**
Set the view of the drop down load
*/
-(void) setView {
currentZPoisition = self.layer.zPosition;
currentTableZPoisition = _optionsTableView.layer.zPosition;
_title.textColor = [UIColor colorWithRed:133.0/255.0 green:141.0/255.0 blue:146.0/255.0 alpha:1.0];
_optionsTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[self setDropDownPosition];
[dropDown setBackgroundColor:[UIColor whiteColor]];
[_optionsTableView setBackgroundColor:[UIColor whiteColor]];
[self setViewBorder:_dropDownButton];
[self addSubview:dropDown];
[self hideOptions];
}
/**
Set a view border
*/
-(void)setViewBorder:(UIView*)view {
view.layer.borderWidth = 0.5;
view.layer.borderColor = [[UIColor colorWithRed:133.0/255.0 green:141.0/255.0 blue:146.0/255.0 alpha:1.0] CGColor];
view.layer.cornerRadius = 5;
}
/**
Set the position of the drop down in the super view
*/
-(void) setDropDownPosition {
CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
dropDown.frame = frame;
}
- (IBAction)dropDownClicked:(UIButton *)sender {
if (isOpen)
[self hideOptions];
else
[self showOptions];
}
/**
Show the options
*/
-(void) showOptions {
float yPosition = _dropDownButton.frame.size.height;
float width = _dropDownButton.frame.size.width;
float selfHeight = dropDown.frame.size.height + _optionsTableView.frame.size.height;
_optionsTableView.frame = CGRectMake(0, yPosition, width, _optionsTableView.frame.size.height);
self.layer.zPosition = 100000;
_optionsTableView.layer.zPosition = 1000000;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, selfHeight);
[self addSubview:_optionsTableView];
[self.superview bringSubviewToFront:self];
[self bringSubviewToFront:_optionsTableView];
isOpen = YES;
}
/**
Hide the options
*/
-(void) hideOptions {
[_optionsTableView removeFromSuperview];
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, _dropDownButton.frame.size.height);
self.layer.zPosition = currentZPoisition;
_optionsTableView.layer.zPosition = currentTableZPoisition;
[self.superview sendSubviewToBack:self];
isOpen = NO;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIColor* grayColor = [UIColor colorWithRed:134.0/255.0 green:142.0/255.0 blue:147.0/255.0 alpha:1];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//set selected background color
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = grayColor;
[cell setSelectedBackgroundView:bgColorView];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.textColor = grayColor;
[cell.textLabel setFont:[cell.textLabel.font fontWithSize:14]];
}
cell.userInteractionEnabled = YES;
if (_dropDownOptions)
cell.textLabel.text = _dropDownOptions[indexPath.row];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dropDownOptions.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_title.text = _dropDownOptions[indexPath.row];
_currentValue = [NSNumber numberWithInteger:indexPath.row];
[self hideOptions];
}
-(void)setSelected:(NSIndexPath *)index
{
[self tableView:_optionsTableView didSelectRowAtIndexPath:index];
_currentValue= [NSNumber numberWithInt: (int)index.row];
}
@end
奇怪的是,如果我用两根手指点击它(WHAT ??),表格中的一个单元格我只会点击下拉选项。 (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
没有被称为其他...我知道有点难以介入某人的代码但有任何想法吗?
这是它关闭时的样子:
并打开: