【问题标题】:Add a button that scrolls with a UITableView in Swift在 Swift 中添加一个使用 UITableView 滚动的按钮
【发布时间】:2018-01-16 20:18:13
【问题描述】:

我正在尝试让 UIButton(或可点击的 UIView)出现在 UITableView 上方。这很容易。困难的部分是当用户向下滚动表格视图时使其滚动。

我尝试过像这样使用 UIScrollView:

private var scroll = UIScrollView();
private var table = UITableView();

override func viewDidLoad()
{
    super.viewDidLoad()

    scroll = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height));

    table = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height));
    table.register(ATableCell.self, forCellReuseIdentifier: "reuseIdentifier");
    table.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0);
    table.backgroundColor = UIColor.clear;
    table.separatorInset = .zero;
    table.layoutMargins = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10);

    table.rowHeight = UITableViewAutomaticDimension;
    // table.estimatedRowHeight = 100;

    table.dataSource = self;
    table.delegate = self;

    scroll.addSubview(table);

    self.view.addSubview(table);

}

顶部的滚动条空间可以容纳一个按钮,但始终存在。我希望它保持在表格视图上方,但在用户向下滚动时失去焦点(如网页或 iphone 中的标准邮件应用程序,它通过搜索栏和表格视图执行此操作)。谢谢。

【问题讨论】:

  • 你试过tableHeaderView吗?
  • 在 UITableView 的页眉和页脚中?我有一些部分,但我不希望标题像它那样留在那里。
  • 如何使用tableHeaderView?感谢您的帮助。
  • 您不能在表格的第一个单元格中添加按钮吗?
  • 设置你的tableview样式Grouped,然后设置tableHeaderView,它将随着tableview滚动。

标签: ios swift uitableview swift3 uiscrollview


【解决方案1】:

您可以使用以下 2 种方法在 tableview 上方添加按钮:

1.部分标题:

在表格视图的最顶部部分添加部分标题。不要忘记将UITableViewStyle 更改为plain,否则部分标题会粘在顶部。

2。表头视图:

添加tableHeaderView 并在此处放置您想要的任何内容。它会在您滚动 tableview 时滚动。

希望对你有帮助。

【讨论】:

    【解决方案2】:

    您可以使用委托方法检测 UITableView 的滚动,您可以在其中设置 UIButton 的位置,以便它可以根据您的 UITableView 滚动移动。

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if (scrollView == myTableView){
       // Your code here.....
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 2015-03-07
      • 2017-06-15
      • 2017-04-13
      • 2017-01-06
      • 2016-04-30
      相关资源
      最近更新 更多