2011-02-17 34 views
3

我有一個UITableViewController這是另一個UITableViewController的詳細視圖。UIDatePicker和UITableView

在此表格上標有「日期」的單元格。使用Apple的「DateCell」示例(http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html),當您觸摸該單元時,我試圖添加一個UIDatePicker

我已經完全按照例子的方式連接了一切。我唯一改變的是,我在圍欄這個didSelectRowAtIndexPath代碼:

if(indexPath.section == 1 && indexPath.row == 0) 
{ 
    //Date picker stuff 
} 

這是爲了確保它只有在按下右邊的單元格時運行。

對於我的生活,我無法得到它的工作。點擊它只會將它變成藍色(選中)。反覆點擊它什麼也不做。

如果我滾動到UITableView的底部,單擊它可以爲白色矩形創建動畫。最終重複點擊最終覆蓋整個表格視圖。

這對我沒有多大意義。請...我怎麼能做到這一點?

如果你想要更多的代碼,我可以提供它,但它幾乎與DateCell代碼相同。我大部分都是複製/粘貼的。

由於提前,

克里夫

+0

沒有人會下載dateCell例子只是爲了回答你的問題,甚至不是我。所以用實際代碼替換// Date拾取器的東西。 – 2011-02-17 05:48:03

回答

6

確保您有IB挑選器對象,如果這是你使用的是什麼,然後創建一個IBOutlet參考,並將其連接到IB的對象。我將我的pickerView設置爲隱藏在IB中,並在需要時使其可見。否則,您可以根據需要簡單地實例化一個。

在您的didSelectRowAtIndexPath中,您可以嘗試下面的代碼,看看會發生什麼。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (**your cell/section selection logic here**) { 
     [self.view endEditing:YES]; // resign firstResponder if you have any text fields so the keyboard doesn't get in the way 
     [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; // Scroll your row to the top so the user can actually see the row when interacting with the pickerView 

     // Pickerview setup 
     [self.typePicker setCenter:CGPointMake(150, 500)]; // place the pickerView outside the screen boundaries 
     [self.typePicker setHidden:NO]; // set it to visible and then animate it to slide up 
     [UIView beginAnimations:@"slideIn" context:nil]; 
     [self.typePicker setCenter:CGPointMake(150, 250)]; 
     [UIView commitAnimations];   
    } 
} 

之後,你需要的,如果你想更新的選擇器視圖的選擇更改您的電池的標籤來實現你pickerView:didSelectRow:方法...

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    // Your code to get the current table view cell and update it with the data from your pickerView 
} 

確保您的viewController被聲明爲代表對於tableView <UITableViewDelegate>以及對於pickerView`'

這應該給你一個良好的開端。讓我知道,如果你有任何問題,等等

乾杯, 羅格

+0

嗯,這就是爲什麼我不應該在深夜發佈StackOverflow。我意識到我遇到問題的原因是因爲我從未將UIDatePicker添加到我的窗口並連接了插座。這是我的第一個iPhone應用程序,直到這一點我已經實例化了代碼中的大多數對象。我只需要爲這個做同樣的事情。你的評論引發了這種認識,所以我給你信用。對不起,浪費你的時間。 – clifgriffin 2011-02-17 13:16:04