【问题标题】:Add same UITableView to different ViewControllers将相同的 UITableView 添加到不同的 ViewControllers
【发布时间】:2018-02-14 10:31:11
【问题描述】:

在我的应用程序中,我有相同的 UI 组件,应该在不同的 ViewController 中呈现。这个组件看起来像按钮列表,点击每个按钮应该会推送带有一些参数的 VievController - 我决定使用 UITableView。

假设我有 ViewControllers: VC1,VC2,VC3,这个 UITableView 应该在其中显示。每个 ViewController 中应该显示的单元格数量存储在名为 buttons 的数组中。

struct Button {
    var title : String
    var param : Int
}

class VC1 : UIViewController {
     var buttons = [Button]()
}

现在我正在每个 ViewController 中创建这个 tableView,比如

class VC1 : UIViewController, UITableViewDelegate, UITableViewDataSource {
     var buttons = [Button]()

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return buttons.count
     }

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     ....
     return btnCell
     }

      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
           performSegue....
      }
}
  • 声明此 TableView 的最佳方式是什么?我应该放在哪里 所有 UITableView 委托方法?
  • 我是否应该使用自定义 XIB 文件为此表创建单独的 UIView 子类
  • 这是创建一些协议 canShowButtons 的好地方吗?如果是这样 - 需要什么变量和方法。

【问题讨论】:

    标签: ios swift uitableview uikit


    【解决方案1】:

    您可以将所有 UI 元素放在 xib 中,并在该 xib 类中处理 tableView 委托。如果你想在 xib 和 VC1 之间进行通信,你可以编写一个协议。或者第二种方法非常相似,您可以创建一个包含所有 UI 元素(tableView、按钮等)的 viewController(我们将其命名为 UIelemntsVC)并将其加载到您的 VC1、VC2、VC3 等中。

    UIelementsVC 类中,您可以像这样控制它的大小

    self.view.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: self.view.frame.width, height: self.view.frame.height*2)
    

    如果你想使用自动布局,你应该在你的 VC1 中放置一个 UIView 并添加UIelementsVC 子视图。

    您可以像这样加载自定义 ViewController:

    let customVC = UIelementsVC()
    customVC.customDelegate = self // Only if you have a custom protocol
    customVC.numberOfButtons = 5 // You can edit vars in customVC like this
    
    self.addChildViewController(customVC)
    self.view.addSubview(customVC)
    customVC(toParentViewController: self)
    

    【讨论】:

      【解决方案2】:

      只需像这样编写您称为VC1 的代码:

      class ButtonTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
      
          ...
      
          // The better is not to make the data as UIButton s,
          //    but as data (Strings/Int/Structs/etc...), for testability
          //    and flexibility.
      
          var dataSource = (() -> (UIButton))? {
              didSet {
                  populateData()
              }
          }
      
          func registerTheCellsFromXIB() {
              // Here try to use register(_:forCellReuseIdentifier:)
          }
      
          override func viewDidLoad() {
              super.viewDidLoad()
              registerTheCellsFromXIB()
              populateData()
          }
      
          func populateData() {
              buttons = dataSource?()
          }
          ...
      
      }
      

      然后你可以为所有这些人制作一个 XIB。如果您希望一个单元格与另一个单元格不同,请为不同类型的单元格制作比 XIB 更多的单元格,然后放置一个可设置的闭包,该闭包可以 registerTheCellsFromXIB() 而不仅仅是一个普通函数(使用为 populateDate 制作的相同技术

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-24
        • 2017-08-08
        • 2021-02-24
        • 1970-01-01
        • 2021-01-10
        • 2014-12-20
        • 2023-04-08
        • 1970-01-01
        相关资源
        最近更新 更多