2011-06-07 3 views
7

私はUIPickerViewを(キーボードのように)ボタンを押したときに表示し、ユーザーが画面上の任意の場所をタップすると表示します。これどうやってするの?ありがとう。キーボードのようにピッカービューを表示するにはどうすればよいですか?

少し背景:UITableViewCellで月と呼ばれるUITextFieldがあります。今私にとって最良の選択肢は、UIPikcerViewをそこに置くことです。ユーザが入力しなくても月を選択するほうが簡単になります(もっと良い方法です)。しかし、UIPickerViewはUITableViewCellで実際には見苦しく見えますが、私はそれを巨大なサイズに変更することはできません。だから私はこの場合何をすべきか?

+0

あなたのタイトルはあまり説明的ではありません。あなたが達成しようとしていることの詳細(タイトル内)をご記入ください – Julian

+0

申し訳ありません。タイトルは他の人によって更新されましたが、私は今この質問の背景についても説明しました。 – sumderungHAY

答えて

13

これは、UITableViewのtextFieldにUIPickerViewを使用する方法です。

そしてtextFieldDidEndEditingで今

- (void)datePickerValueChangedd:(UIDatePicker*) datePicker{ 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)]; 
NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
df.dateStyle = NSDateFormatterMediumStyle; 
label.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]]; 

    NSLog(@"I am inside the datePickerValueChanged - - %@", label.text); 
[df release]; 
    globalValue1 = label.text; 

    // use a global variable to copy the value from the pickerView. // 
    }  

datePickerValueChangedd

ため

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    cell.selectionStyle= UITableViewCellSelectionStyleNone; 
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)]; 
    textField.clearsOnBeginEditing = NO; 
    textField.textAlignment = UITextAlignmentRight; 
    textField.delegate = self; 
    [cell.contentView addSubview:textField]; 
    //textField.returnKeyType = UIReturnKeyDone; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 

     // if you want a UIPickerView for first row... then do the following // 
     // Here.. packSizePickerView is an object of UIPickerView 

    if (indexPath.row == 1) 
    { 
    textField.tag=0; 
    textField.delegate= self; 
    packSizePickerView.delegate = self; 
    packSizePickerView = [[UIPickerView alloc] init]; 
    packSizePickerView.autoresizingMask=UIViewAutoresizingFlexibleWidth; 
    packSizePickerView.showsSelectionIndicator=YES; 
    textField.inputView = packSizePickerView; 
    } 

    // if you want to select date/months in row 2, go for UIDatePickerView // 

    if (indexPath.row == 1) 
    { 
     textField.tag = 1; 
     UIDatePicker *datePicker = [[UIDatePicker alloc] init]; 
     datePicker.datePickerMode = UIDatePickerModeDate; 
     [datePicker addTarget:self action:@selector(datePickerValueChangedd:) forControlEvents:UIControlEventValueChanged]; 
     datePicker.tag = indexPath.row; 
     textField.delegate= self; 
     textField.inputView = datePicker; 
     [datePicker release]; 

    } 

} 

// Configure the cell... 

NSInteger row = [indexPath row]; 
cell.textLabel.text = [myArray objectAtIndex:row]; 

return cell; 

}:

-(void) textFieldDidEndEditing:(UITextField *)textField { 
if (textField.tag ==0) 
    [textField setText: globalValue0]; 

if (textField.tag ==1) 
    [textField setText: globalValue1]; 

} 

そしてUIDatePickerを辞任することは、UicontrolのようUIViewのを設定し、

resignFirstResponder 
+0

偉大な答え!それはまさに私が探していたものです! +1 – Garoal

0

UIPickerViewを表示可能な画面の外側に配置し、ボタンを押したときにアニメーション表示する必要があると思います。私は本当にそれを取り除くための最良の方法は何だろうか分かりませんが、ピッカーが表示されているときに親ビューのタップを聞いて、それを元に戻すことができます。

関連する問題