2011-06-07 50 views
7

Tôi muốn một UIPickerView hiển thị khi tôi nhấn một nút (giống như bàn phím) và sau đó biến mất khi người dùng chạm bất cứ nơi nào trên màn hình. Tôi có thể làm cái này như thế nào? Cảm ơn.Làm cách nào để hiển thị chế độ xem bộ chọn giống như bàn phím?

Nền hơn một chút: Tôi có UITextField được gọi là tháng trong UITableViewCell. Bây giờ lựa chọn tốt nhất cho tôi là đặt một UIPikcerView ở đó và nó sẽ dễ dàng hơn cho người dùng chỉ cần chọn tháng thay vì phải gõ nó ra (và đó là cách tốt hơn). Nhưng UIPickerView trông thực sự xấu xí trong một UITableViewCell, chưa kể tôi không thể thay đổi kích thước khổng lồ của nó. Vậy tôi nên làm gì trong trường hợp này?

+0

Tiêu đề của bạn không phải là rất mô tả. Vui lòng cung cấp thêm chi tiết về những gì bạn đang cố gắng đạt được (trong tiêu đề) – Julian

+0

Xin lỗi về điều đó. Tiêu đề đã được người khác cập nhật, nhưng bây giờ tôi cũng đã cung cấp thêm thông tin cơ bản cho câu hỏi này. – sumderungHAY

Trả lời

13

Đây là cách bạn nên sử dụng UIPickerView cho textField trong UITableView.

- (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; 

}

Và đối với datePickerValueChangedd

- (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. // 
    }  

Bây giờ trong textFieldDidEndEditing:

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

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

} 

Và phải từ chức UIDatePicker, thiết lập các UIView như một UIControl và

resignFirstResponder 
+0

Câu trả lời hay !! Đó chính xác là những gì tôi đang tìm kiếm !! +1 – Garoal

0

Tôi nghĩ rằng bạn cần đặt UIPickerView của mình "bên ngoài" màn hình có thể xem và tạo hiệu ứng khi nhấn nút. Tôi không thực sự chắc chắn cách tốt nhất để thoát khỏi nó sẽ là gì nhưng bạn có thể có thể nghe một lần nhấn vào chế độ xem gốc khi bộ chọn hiển thị và sau đó tạo hoạt ảnh trở lại.

Các vấn đề liên quan