2

テーブルビューコントローラーがあり、セルの1つが「日付」で、クリックすると日付ピッカーを表示して値を更新します。

日付セルを含むテーブル

制限日をクリックすると、

日付ピッカー付きのテーブル

コードは次のとおりです。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ( indexPath.section == 0 )
    {
        if ( indexPath.row == 3 )
        {
            UITableViewCell *targetCell = [tableView cellForRowAtIndexPath:indexPath];
            self.pickerView.date = [self.dateFormatter dateFromString:targetCell.detailTextLabel.text];

            // check if our date picker is already on screen
            if (self.pickerView.superview == nil)
            {
                [self.view.window addSubview: self.pickerView];
                // size up the picker view to our screen and compute the start/end frame origin for our slide up animation
                //
                // compute the start frame

                CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
                CGSize pickerSize = [self.pickerView sizeThatFits:CGSizeZero];
                CGRect startRect = CGRectMake(0.0,
                                              screenRect.origin.y + screenRect.size.height,
                                              pickerSize.width, pickerSize.height);
                self.pickerView.frame = startRect;

                // compute the end frame
                CGRect pickerRect = CGRectMake(0.0,
                                               screenRect.origin.y + screenRect.size.height - pickerSize.height,
                                               pickerSize.width,
                                               pickerSize.height);
                // start the slide up animation
                [UIView beginAnimations:nil context:NULL];
                    [UIView setAnimationDuration:0.3];

                    // we need to perform some post operations after the animation is complete
                    [UIView setAnimationDelegate:self];

                    self.pickerView.frame = pickerRect;

                    // shrink the table vertical size to make room for the date picker
                    CGRect newFrame = self.tableView.frame;
                    newFrame.size.height -= self.pickerView.frame.size.height;
                    self.tableView.frame = newFrame;
                [UIView commitAnimations];

                // add the "Done" button to the nav bar

                self.navigationItem.rightBarButtonItem = self.doneButton;
            }
        }
    }
}


- (IBAction)dateChanged:(id)sender
{
    [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]].detailTextLabel.text = [self.dateFormatter stringFromDate:[sender date]];
}

- (void)slideDownDidStop
{
    [self.pickerView removeFromSuperview];
}

- (IBAction)doneAction:(id)sender
{
    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    CGRect endFrame = self.pickerView.frame;
    endFrame.origin.y = screenRect.origin.y + screenRect.size.height;

    // start the slide down animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    // we need to perform some post operations after the animation is complete
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(slideDownDidStop)];

    self.pickerView.frame = endFrame;
    [UIView commitAnimations];

    // grow the table back again in vertical size to make room for the date picker
    CGRect newFrame = self.tableView.frame;
    newFrame.size.height += self.pickerView.frame.size.height;
    self.tableView.frame = newFrame;

    // remove the "Done" button in the nav bar
    self.navigationItem.rightBarButtonItem = nil;

    // deselect the current table row
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    self.navigationItem.rightBarButtonItem = self.nextButton;
}

3 つの質問があります。

最初: datepicker の上にある黒いバーは何ですか?どうすれば削除できますか?

2番目: ユーザーが日付ピッカーの外をクリックしたときに日付ピッカーを閉じるにはどうすればよいですか?

3番目:datepickerが開いたときにフィールド「制限日」が表示されるように、テーブルビューのスクロールを移動するにはどうすればよいですか?

大きなスクリーンショットで申し訳ありません。

4

1 に答える 1

1

UIActionSheetで日付ピッカーをモ​​ーダルに表示するのはどうですか?

上記の例では、その黒いバー(おそらくサイズの問題です)が、「保存」や「キャンセル」などのボタンを配置できる便利なツールバーに置き換えられています。を使用してそのUIActionSheet外側をクリックすると、自動的に却下されます(これについてはよくわかりません)が、そうでない場合は、そのdismissメソッドを使用して非表示にすることができます。

テーブルビューについては、必要[myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:someRow inSection:someSection] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];な特定のセルでテーブルスクロールのオフセットを設定するために使用できます。

于 2012-09-12T15:15:15.487 に答える