UIDatePicker和UITableView

cli*_*fin 3 iphone cocoa-touch objective-c uidatepicker uitableview

我有一个UITableViewController是另一个的详细视图UITableViewController.

在该表上是标有"日期"的单元格.使用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
}
Run Code Online (Sandbox Code Playgroud)

这是为了确保它仅在按下正确的单元格时运行.

对于我的生活,我无法让它发挥作用.单击它只会将其变为蓝色(已选中).反复点击它什么都不做.

如果我滚动到UITableView的底部,单击它会动画一个白色矩形.重复单击最终会覆盖整个表格视图.

这对我来说没有多大意义.请..我可以这样做吗?

如果你想要更多代码,我可以提供它,但它几乎与DateCell代码相同.我大部分都是复制/粘贴的.

提前致谢,

克里夫

Rog*_*Rog 6

如果您使用的是IB,请确保在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];        
    }
}
Run Code Online (Sandbox Code Playgroud)

之后,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
}
Run Code Online (Sandbox Code Playgroud)

确保你的viewController被声明为tableView的委托<UITableViewDelegate>以及pickerView的委托`

这应该会给你一个良好的开端.如果您有任何疑问,请告诉我.

干杯,罗格