2014-09-25 60 views
0

我有一個UITableview與圖像按鈕。當點擊按鈕時,它將動態添加子視圖(表格視圖),如下圖所示的下拉列表。我無法選擇下拉列表中的第二個選項(表格視圖)。在iOS中:無法觸摸子視圖中的UITableview

問題是下拉高度超過了表格行,所以我無法選擇第二個選項。

任何人都有解決方案嗎?或任何想法顯示在UITableView下拉?

table view with dropdown

編輯:

UITextField做工精細dropdown.but不是在UITableView的工作。

下拉代碼:

- (id)showDropDown:(UIButton *)b:(CGFloat *)height:(NSArray *)arr:(NSString *)direction { 
    btnSender = b; 
    animationDirection = direction; 
    self.table = (UIButton *)[super init]; 
    if (self) { 
     // Initialization code 
     CGRect btn = b.frame; 
     self.list = [NSArray arrayWithArray:arr]; 
     if ([direction isEqualToString:@"up"]) { 
      self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0); 
      self.layer.shadowOffset = CGSizeMake(-5, -5); 
     }else if ([direction isEqualToString:@"down"]) { 
      self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0); 
      self.layer.shadowOffset = CGSizeMake(-5, 5); 
     } 

     self.layer.masksToBounds = NO; 
     self.layer.cornerRadius = 8; 
     self.layer.shadowRadius = 5; 
     self.layer.shadowOpacity = 0.5; 

     table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btn.size.width+btn.size.width, 0)]; 
     table.delegate = self; 
     table.dataSource = self; 
     table.layer.cornerRadius = 5; 
     table.backgroundColor = [UIColor whiteColor]; 
     table.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
     table.separatorColor = [UIColor grayColor]; 
     table.separatorInset=UIEdgeInsetsZero; 
     table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.5]; 
     if ([direction isEqualToString:@"up"]) { 
      self.frame = CGRectMake(btn.origin.x, btn.origin.y-*height, btn.size.width, *height); 
     } else if([direction isEqualToString:@"down"]) { 
      self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, *height); 
     } 
     table.frame = CGRectMake(0, 0, btn.size.width, *height); 
     [UIView commitAnimations]; 
     [b.superview addSubview:self]; 
     [self addSubview:table]; 
    } 
    return self; 
} 

-(void)hideDropDown:(UIButton *)b { 
    CGRect btn = b.frame; 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    if ([animationDirection isEqualToString:@"up"]) { 
     self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width+btn.size.width, 0); 
    }else if ([animationDirection isEqualToString:@"down"]) { 
     self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width+btn.size.width, 0); 
    } 
    table.frame = CGRectMake(0, 0, btn.size.width, 0); 
    [UIView commitAnimations]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellText = @"Go get some text for your cell."; 
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:10.0]; 
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); 
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByCharWrapping]; 

    return labelSize.height + 20; 
    //return 40; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.list count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.textLabel.font = [UIFont systemFontOfSize:15]; 
     cell.textLabel.textAlignment = NSTextAlignmentCenter; 
     cell.textLabel.lineBreakMode = NSLineBreakByCharWrapping; 
     cell.textLabel.numberOfLines = 0; 
     cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:13.0]; 
    } 
    cell.textLabel.text =[list objectAtIndex:indexPath.row]; 

    cell.textLabel.textColor = [UIColor blackColor]; 

    UIView * v = [[UIView alloc] init]; 
    v.backgroundColor = [UIColor grayColor]; 
    cell.selectedBackgroundView = v; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [self hideDropDown:btnSender]; 

    UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath]; 


    [self myDelegate:indexPath.row]; 
} 

- (void) myDelegate:(NSInteger) index{ 
    [self.delegate niDropDownDelegateMethod:self selectedIndex:index]; 
} 

按鈕TableView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 


    static NSString *simpleTableIdentifier = @"quotelist"; 

    QuoteListCell *cell = (QuoteListCell *)[tableView dequeueReusableCellWithIdentifier: simpleTableIdentifier]; 

    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"QuoteListCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.quoteButton.tag=indexPath.row; 
    [cell.quoteButton addTarget:self action:@selector(dropDownButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 


} 

-(void)dropDownButtonClicked:(UIButton *)sender{ 
    NSLog(@"searchData row at index:%d",sender.tag); 
    UIButton *btn=(UIButton *)sender; 

    if(dropDown == nil) { 
     //[self.quoteListTableView setAllowsSelection:NO]; 

     CGFloat f = 200; 
     dropDown = [[MyDropDown alloc]showDropDown:btn :&f :[[NSArray alloc]initWithObjects:@"Dublicate",@"Create Order", nil] :@"down"]; 
     dropDown.delegate = self; 
    } 
    else { 
     //[self.quoteListTableView setAllowsSelection:YES]; 
     [dropDown hideDropDown:btn]; 
     dropDown=nil; 
    } 
} 
+0

你可以分享添加下拉 – 2014-09-25 05:39:06

+0

下拉代碼的代碼是共享的。 – Chidhambaram 2014-09-25 06:00:06

+0

一段時間增加按鈕寬度。並且在那裏添加了按鈕。並且還分享了cellforrowatindex代碼的代碼。 – 2014-09-25 06:04:55

回答

0
- (id)showDropDown:(UIButton *)b:(CGFloat *)height:(NSArray *)arr:(NSString *)direction { 
    btnSender = b; 
    animationDirection = direction; 
    self.table = (UIButton *)[super init]; 
    if (self) { 
     // Initialization code 
     CGRect btn = b.frame; 
     self.list = [NSArray arrayWithArray:arr]; 
     if ([direction isEqualToString:@"up"]) { 
      self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0); 
      self.layer.shadowOffset = CGSizeMake(-5, -5); 
     }else if ([direction isEqualToString:@"down"]) { 
      self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0); 
      self.layer.shadowOffset = CGSizeMake(-5, 5); 
     } 

     self.layer.masksToBounds = NO; 
     self.layer.cornerRadius = 8; 
     self.layer.shadowRadius = 5; 
     self.layer.shadowOpacity = 0.5; 

     table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btn.size.width+btn.size.width, 0)]; 
     table.delegate = self; 
     table.dataSource = self; 
     table.layer.cornerRadius = 5; 
     table.backgroundColor = [UIColor whiteColor]; 
     table.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
     table.separatorColor = [UIColor grayColor]; 
     table.separatorInset=UIEdgeInsetsZero; 
     table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 
     [UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:0.5]; 
     if ([direction isEqualToString:@"up"]) { 
      self.frame = CGRectMake(btn.origin.x, btn.origin.y-*height, btn.size.width, *height); 
     } else if([direction isEqualToString:@"down"]) { 
      self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, *height); 
     } 
     table.frame = CGRectMake(0, 0, btn.size.width, *height); 
     [UIView commitAnimations]; 
     [b.superview.superview addSubview:self]; 
     [self addSubview:table]; 
    } 
    return self; 
} 

I have changed one line in the above code, use this. 
+0

你改變了哪一行? – Chidhambaram 2014-09-25 06:32:22

+0

[b.superview.superview addSubview:self]; – 2014-09-25 06:32:57

+0

是你的問題解決了。@ Chidhambaram – 2014-09-25 06:39:18

相關問題