【问题标题】:UIDatePicker and a UITableViewUIDatePicker 和一个 UITableView
【发布时间】:2011-02-17 04:58:43
【问题描述】:

我有一个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
}

这是为了确保它只在按下右侧单元格时运行。

对于我的生活,我无法让它工作。单击它只会将其变为蓝色(选中)。反复单击它没有任何作用。

如果我滚动到 UITableView 的底部,单击它会激活一个白色矩形。反复点击最终会覆盖整个表格视图。

这对我来说没有多大意义。请问..我该怎么做?

如果您需要更多代码,我可以提供,但它实际上与 DateCell 代码相同。我大部分都是复制/粘贴的。

提前致谢,

悬崖

【问题讨论】:

  • 没有人会下载 dateCell 示例只是为了回答您的问题,甚至我也不会。所以用真实代码替换//Date picker stuff

标签: iphone objective-c cocoa-touch uitableview uidatepicker


【解决方案1】:

如果您正在使用,请确保您在 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 `'的委托

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

干杯, 罗格

【讨论】:

  • 好吧,这就是为什么我不应该在深夜发帖到 StackOverflow 上。我意识到我遇到问题的原因是因为我从未将 UIDatePicker 添加到我的窗口并连接了插座。这是我的第一个 iPhone 应用程序,到目前为止,我已经用代码实例化了大多数对象。我只需要为这个做同样的事情。你的评论引发了这种认识,所以我给你信用。很抱歉浪费了您的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多