2014-10-28 72 views
0

在tableview按钮点击创建UIpickerview。当第一次点击每个按钮时,选取器要显示。如果我在另一个时间点击相同的按钮,选择器想要隐藏并获取选择器值。在这里,我把我的代码只有最后一个单元索引完成过程第一个单元格索引不工作?我该如何解决这个问题帮助我!这里屏幕!在实现代码如下桌面视图按钮点击需要在iOS中pickerview

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [_arr_tags count]; /// in tag array number of button 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:nil]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 


     /// create a button 

     UIImage *ButtonImagecmt = [UIImage imageNamed:@"[email protected]"]; 
     UIButton *myButtoncmt = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     myButtoncmt.frame = CGRectMake(10,50,90,35); 
     [myButtoncmt setBackgroundImage:ButtonImagecmt forState:UIControlStateNormal]; 
     [myButtoncmt addTarget:self action:@selector(picker_select:)forControlEvents:UIControlEventTouchDown]; 

     [cell addSubview:myButtoncmt]; 

     flg=1; /// first time picker want to hide so flag set 1. 




     myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(120, 0, 160, 180)]; 
     myPickerView.delegate = self; 
     myPickerView.showsSelectionIndicator = YES; 
     myPickerView.hidden=true; 
     [cell addSubview:myPickerView]; 

    } 


    return cell; 
} 
- (IBAction)picker_select:(id)search 
{ 
    if (flg==1) 
    { 
     /// first time button clickt picker want to display 
     myPickerView.hidden=false; 
     flg=0; 
    } 
    else 
    { 
     /// secound time button click picker want to hide 
     myPickerView.hidden=true; 
     flg=1; 
    } 

} 

'和接取此代码仅在最后一个单元格工作; 创建全球像

UIPickerView * myPickerView捡拾工具。要在所有的细胞工作表视图

enter image description here

+0

而不是检查'flg'使用按钮选择状态。我希望这会更好。在这里它不工作,因为你只有一个'flg'变量的所有单元格。 – Exploring 2014-10-28 11:36:00

+0

我如何检查每个按钮在表格单元格中的单击的flg。如何隐藏和显示每个单元格按钮中的pickerview点击tableview帮助我。 – Shalini 2014-10-28 11:44:48

+0

在您的IBAction尝试重新加载tableView,因为此刻,您正在设置myPickerView.hidden,但是我看不到您正在刷新tableView。希望这可以帮助。 – Gismay 2014-10-28 12:05:18

回答

1

首先让我以后可以解释你的错误,我会建议你其他的方式。 您正在声明UIPickerView * myPickerView;在出以下方法

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

的侧但要初始化myPickerView 的下列方法

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

,并再次您尝试访问以下方法myPickerView

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

s意味着对象myPickerView代表最后一个单元格pickerview。所以在表格视图中点击的按钮,myPickerView总是表示最后一个单元格pickerview。根据可重用性概念,在屏幕上加载最后一个单元格之前,您无法看到此选取器。 所以我的建议你与你的代码

集索引行的标签中的cellForRowAtIndexPath方法

myButtoncmt.tag = indexpath.row; 

现在用同样的方法设置标签的工作,以您的按钮状波纹管你pickerview也(我不推荐这个,但我试图解决您的代码问题),如波纹管

myPickerView.tag = [_arr_tags count]+indexpath.row; 

现在 - (IBAction为)picker_select:(ID)搜索访问,使用下面的代码

UIButton *tempBtn =(UIButton *) search; 
UITableViewCell *cell= [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tempBtn.tag inSection:0]] 
UIPickerView *pickerView = [cell viewWithTag:[_arr_tags count]+tempBtn.tag]; 
pickerView.hidden = !pickerView.isHidden; 

现在你可以看到所有的选择。试着让我知道你的结果

+0

如果我点击第一个单元,但选择器想要显示。我再次点击相同的按钮,希望隐藏。这可以通过tableview中的所有单元完成。 – Shalini 2014-10-28 13:28:29

+0

用同样的方法想创建一个标签并设置标签值 – Shalini 2014-10-29 05:13:58

+0

对不起,我不理解你的问题。请描述你的问题 – 2014-10-29 05:51:07