2014-10-28 37 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