简体   繁体   English

UIDatePicker和UITableView

[英]UIDatePicker and a UITableView

I have a UITableViewController that is the detail view for another UITableViewController . 我有一个UITableViewController ,它是另一个UITableViewController的详细视图。

On this table is a cell labeled "Date". 在该表上是标有“日期”的单元格。 Using Apple's "DateCell" example ( http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html ), I have tried to add a UIDatePicker when you touch the cell. 使用Apple的“DateCell”示例( http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html ),我试图在您触摸单元格时添加UIDatePicker

I have wired up everything exactly the way the example does. 我完全按照示例的方式连接了所有内容。 The only thing I've changed is that I fenced the didSelectRowAtIndexPath code in this: 我唯一改变的是我在这里用didSelectRowAtIndexPath代码进行了操作:

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

This is to ensure it only runs when the right cell is pressed. 这是为了确保它仅在按下正确的单元格时运行。

For the life of me, I can't get it to work. 对于我的生活,我无法让它发挥作用。 Clicking it does nothing but turn it blue (selected). 单击它只会将其变为蓝色(已选中)。 Clicking it repeatedly does nothing. 反复点击它什么都不做。

If I scroll to the bottom of the UITableView, clicking it animates up a white rectangle. 如果我滚动到UITableView的底部,单击它会动画一个白色矩形。 Clicking repeatedly eventually covers the whole table view. 重复单击最终会覆盖整个表格视图。

It doesn't make a lot of sense to me. 这对我来说没有多大意义。 Please..how can I do this? 请..我可以这样做吗?

If you want more code, I can provide it, but it's virtually identical to the DateCell code. 如果你想要更多代码,我可以提供它,但它几乎与DateCell代码相同。 I copied/pasted for the most part. 我大部分都是复制/粘贴的。

Thanks in advance, 提前致谢,

Clif 克里夫

Make sure you have a picker object in IB if that's what you are using, then create an IBOutlet reference and connect it to the IB object. 如果您使用的是IB,请确保在IB中有一个选择器对象,然后创建一个IBOutlet引用并将其连接到IB对象。 I set my pickerView to hidden in IB and make it visible when required. 我将pickerView设置为隐藏在IB中,并在需要时使其可见。 Otherwise you can simply instantiate one as needed. 否则,您可以根据需要简单地实例化一个。

In your didSelectRowAtIndexPath, you can try the code below and see what happens. 在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];        
    }
}

After that you need to implement your pickerView:didSelectRow: method if you want to update the label of your cell as the picker view selection changes... 之后,如果要在选择器视图选择发生变化时更新单元格的标签,则需要实现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
}

Make sure your viewController is declared as delegate for the tableView <UITableViewDelegate> as well as for the pickerView `' 确保你的viewController被声明为tableView <UITableViewDelegate>委托以及pickerView`'的委托。

This should give you a good head start. 这应该会给你一个良好的开端。 Let me know if you have any questions etc. 如果您有任何疑问,请告诉我。

Cheers, Rog 干杯,罗格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM