简体   繁体   English

将UIPickerView设置为UITableview的底部,而不考虑滚动位置

[英]Set a UIPickerView to bottom of UITableview regardless of scroll position

I'd like to present a UIPickerView snapped to the bottom of a UITableView regardless of where it is scrolled. 我想呈现一个UIPickerView ,它固定在UITableView的底部,无论它在何处滚动。

Currently I have a UIPickerView added to a UITableView that I present when a button is pressed, but when I scroll the table the UIPickerView goes out of view, and if I'm scrolled out of range of where I've presented it, the UIPickerView appears to have never been called. 目前,我有一个添加到UITableViewUIPickerView ,当按下按钮时会显示该UITableView ,但是当我滚动表时, UIPickerView不在视图范围内;如果滚动到显示范围之外, UIPickerView似乎从未被调用过。

Does anyone know how to accomplish this? 有谁知道如何做到这一点?

Thank 谢谢

The use of UITableViewController is great unless you need to add subview that don't scroll with the table view. 除非您需要添加不随表格视图滚动的子视图,否则UITableViewController的用法非常有用。 It can't really be done. 真的做不到。 The solution is to use a UIViewController instead and add your own table view, setup the table view dataSource and delegate protocols and replicate the basic table view controller plumbing. 解决方案是改用UIViewController并添加自己的表视图,设置表视图dataSource和委托协议,并复制基本的表视图控制器管道。

Once your view controller works like a normal table view controller again, you can now add subviews to the view controller's view that won't scroll with the table. 一旦视图控制器再次像普通的表视图控制器一样工作,现在可以将不随表滚动的子视图添加到视图控制器的视图中。

if u add tableview programatically 如果您以编程方式添加tableview

    SearchtableView.frame = CGRectMake(450, 90, 318, 600);
            SearchtableView  =  [[UITableView alloc] initWithFrame:CGRectMake(623, 90, 400, 400)style:UITableViewStyleGrouped];
            [self.view addSubview:SearchtableView];
//belove the tableview//
//Add UIPickerView to the self.view]//

Something told me that there must be a simpler alternative to the answers that I'd been given, and I found a solution. 有人告诉我,除了给出的答案之外,必须有一个更简单的替代方法,于是我找到了解决方案。

I decided to find the y coordinate of the scroll point using scrollViewDidScroll: and then animating the UIPickerView to the desired location. 我决定使用scrollViewDidScroll:找到滚动点的y坐标,然后将UIPickerView设置为所需的位置。

    ...
    [UIView beginAnimations:nil context:NULL];
     CGFloat pointY = self.scrollPoint.y;
    [self.sortPicker setFrame:CGRectMake(0, pointY + 200, 320, 216)];
    [UIView commitAnimations];
    ...

The UIPickerView will only appear if the scroll has been stopped so it's necessary to implement a method to stop the scroll on touch: 仅当滚动已停止时才会显示UIPickerView,因此有必要实现一种方法来停止触摸时滚动:

- (void)stopScroll:(UITableView *)tableview
{
    CGPoint offset = tableview.contentOffset;
    [tableview setContentOffset:offset animated:NO];
}

This will allow you to display and dismiss the UIPickerView at any moment at any given destination. 这将使您可以随时在任何给定的目的地显示和关闭UIPickerView。

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

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