2011-09-08 62 views
0

我使用Apple的代碼示例來顯示一個UIDatePicker,當一個特定的UITableViewCell被選中。UIDatePicker不會顯示後,UITableViewCell選擇

我該如何改變這種行爲?

Messed up view

我的代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

if(indexPath.section == 5) { 
    //Date Picker 
    UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath]; 
    self.pickerView.date = [self.dateFormatter dateFromString:targetCell.textLabel.text]; 
    if (self.pickerView.superview == nil) 
    { 
     [self.view.window addSubview: self.pickerView]; 

     // size up the picker view to our screen and compute the start/end frame origin for our slide up animation 
     // 
     // compute the start frame 
     CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; 
     CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero]; 
     CGRect startRect = CGRectMake(0.0, 
             screenRect.origin.y + screenRect.size.height, 
             pickerSize.width, pickerSize.height); 
     self.pickerView.frame = startRect; 

     // compute the end frame 
     CGRect pickerRect = CGRectMake(0.0, 
             screenRect.origin.y + screenRect.size.height - pickerSize.height, 
             pickerSize.width, 
             pickerSize.height); 
     // start the slide up animation 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.3]; 

     // we need to perform some post operations after the animation is complete 
     [UIView setAnimationDelegate:self]; 

     self.pickerView.frame = pickerRect; 

     // shrink the table vertical size to make room for the date picker 
     CGRect newFrame = self.tableView.frame; 
     newFrame.size.height -= self.pickerView.frame.size.height; 
     self.tableView.frame = newFrame; 
     [UIView commitAnimations]; 

    } 

} 


} 
+0

爲什麼使用這一行 [tableView deselectRowAtIndexPath:indexPath animated:YES]; – Scar

+0

因爲我的其他單元格是TextFields,我不想讓單元格始終突出顯示。但是這條線沒有任何區別。 – jussi

+0

要禁用單元格上的突出顯示,您必須使用此分類 ''cell setSelectionStyle:UITableViewCellSelectionStyleNone];' 並確保您選擇第5部分 – Scar

回答

0

假設您所提供的代碼是你detail的ViewController的,你不需要設置框架pickerRect。只需要告訴應用程序,當方向的變化,應該pickerView用它旋轉,如下

self.picker.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
UIViewAutoresizingFlexibleTopMargin; 

你可以通過設置它的框架強制選擇器視圖的大小,因爲你所做的一切。由於您使用的是UISplitViewController,因此pickerView必須是您的detail ViewController的子視圖,而不是self.view.window。另外,請通過this link提供的示例查看您正在製作的其他錯誤。

0

我從給定的圖片中看到的,是你正試圖從向上屏幕底部滑出的UIPickerView。

如果是這樣,那麼我認爲你不應該像你應該設置框架。

View and Window Architecture所示,座標系的原點是左上角。

我做對子級是這樣的:

CGRect pickerFrame = pickerView.frame; 
pickerFrame.origin.x = leftTableViewWidth; 
pickerFrame.origin.y = screenFrame.size.height + pickerFrame.size.height; 
pickerView.frame = pickerFrame; 
// begin animation here 
pickerFrame.origin.y += pickerFrame.size.height; 
// commit animation 
相關問題