【问题标题】:Make a TableView only take up part of the screen让 TableView 只占屏幕的一部分
【发布时间】:2013-08-30 15:06:48
【问题描述】:

默认情况下,当我在 IB 中拖出 TableViewController 时,tableview 会占据整个屏幕,并且即使用户滚动 tableview 也无法添加保持原位的 UIView(如果我说添加带有一些按钮,然后当我滚动 UIView 的 tableview 时也会滚动。)

我的程序使用 UITableViewController 子类来控制 tableview,让顶部栏即使在用户滚动时也保持静态的最简单方法是什么?

【问题讨论】:

    标签: ios uitableview uiview interface-builder


    【解决方案1】:

    简单,而不是使用 UITableViewController 只需使用 UIViewController 并在其中放置 UITableView。 (为您在表格上方或下方添加辅助视图留出空间)这样,您可以让视图控制器的视图占据整个屏幕,并在其中手动定义表格视图的框架。需要进行一些重新安排,但会满足您的需求。

    您需要注意的唯一真正区别是,您需要使您的视图控制器同时符合 UITableViewDelegate 和 UITableViewDatasource 协议,并自己声明表格视图的出口。

    @property (weak,nonatomic) IBOutlet UITableView *tableView;
    

    同样重要的是要记住,有一些功能仅在 UITableViewController 上可用,例如 clearsSelectionOnViewWillAppear,但您始终可以自己复制它们。

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [self.tableView deselectRowAtIndexPath:self.tableView.indexPathsForSelectedRows animated:YES];
    }
    

    【讨论】:

    • 我设置了一个 viewController,里面有 tableView 并在顶部为我的菜单留了空间,我只在 IB 中构建了它(没有设置它的类或任何东西)但是当我运行应用程序,tableview 出现“在我打开的空间下”,所以如果我在开放空间中有一个按钮 - 该按钮将出现在 tableview 单元格的顶部
    • 我之前的评论 - 似乎是 IB 中的约束导致它的问题。
    • 请记住另一个问题:您将无法在普通 UITableView 中使用静态表格视图单元格。见stackoverflow.com/questions/22364230/…
    【解决方案2】:

    我推荐的方法(我认为这是 Apple 鼓励的设计模式)是使用嵌入 segue 将您的表视图控制器插入到包含您的附加视图的父控制器中。这有助于保持模块化,并有助于使整个应用程序结构易于理解。

    基本步骤如下:

    1. 在 IB 中创建父视图控制器并添加容器视图
    2. 通过从容器视图按住 ctrl 并拖动到要嵌入的视图控制器来创建嵌入转场。
    3. 为 segue 指定一个标识符,例如“EmbedMyTable”。
    4. 在父控制器的prepareForSegue 方法中,检查您的embed segue 标识符并进行任何其他接线。视图控制器和视图关系是自动建立的,但您可能需要配置表视图控制器并保留对它的引用。

    【讨论】:

      【解决方案3】:

      以编程方式,使用 Swift 约束。在我看来,这个解决方案更干净:

          import UIKit
      
          class SettingsController : UIViewController, UITableViewDelegate, UITableViewDataSource {
      
          let tableView : UITableView = {
              let t = UITableView()
              t.translatesAutoresizingMaskIntoConstraints = false
              return t
          }()
      
          override func viewDidLoad() {
              super.viewDidLoad()
      
              // set a background color so we can easily see the table
              self.view.backgroundColor = UIColor.blue
      
              // add the table view to self.view
              self.view.addSubview(tableView)
      
              // constrain the table view to 120-pts on the top,
              //  32-pts on left, right and bottom (just to demonstrate size/position)
      
              tableView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 32.0).isActive = true
              tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 120.0).isActive = true
              tableView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -32.0).isActive = true
              tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -32.0).isActive = true
      
              // set delegate and datasource
              tableView.delegate = self
              tableView.dataSource = self
      
              // register a defalut cell
              tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
          }
      
          // Note: because this is NOT a subclassed UITableViewController, 
          // DataSource and Delegate functions are NOT overridden
      
          // MARK: - Table view data source
      
          func numberOfSections(in tableView: UITableView) -> Int {
              return 1
          }
      
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return 25
          }
      
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
      
              cell.textLabel?.text = "\(indexPath)"
      
              return cell
          }
      
          // MARK: - Table view delegate
      
          func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
              // etc
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多