2011-02-17 32 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