Ios 将相同的UITableView添加到不同的ViewController
在我的应用程序中,我有相同的UI组件,应该在不同的ViewController中显示。这个组件看起来像是一个按钮列表,点击每个按钮应该会用一些参数按下VievController-我决定使用UITableView 假设我有ViewControllers:VC1、VC2、VC3,其中应该显示此UITableView。每个ViewController中应显示的单元格数存储在名为按钮的数组中 现在我在每个ViewController中创建这个tableView,就像Ios 将相同的UITableView添加到不同的ViewController,ios,swift,uitableview,uikit,Ios,Swift,Uitableview,Uikit,在我的应用程序中,我有相同的UI组件,应该在不同的ViewController中显示。这个组件看起来像是一个按钮列表,点击每个按钮应该会用一些参数按下VievController-我决定使用UITableView 假设我有ViewControllers:VC1、VC2、VC3,其中应该显示此UITableView。每个ViewController中应显示的单元格数存储在名为按钮的数组中 现在我在每个ViewController中创建这个tableView,就像 class VC1 : UIVi
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子类
- 创建一些协议显示按钮的好地方吗?如果是的话,需要什么变量和方法
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)
您可以将所有UI元素放在xib中,并在该xib类中处理tableView委托。如果您想在xib和VC1之间通信,可以编写一个协议。或者第二种方法非常类似,您可以创建一个viewController(命名为UIelemntsVC),其中包含所有UI元素(tableView、按钮等),并将其加载到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)
只需将您调用的
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,然后放置一个可设置的闭包,该闭包可以注册CellsFromxib(),而不仅仅是一个普通函数(使用为
populateDate
所做的相同技术,只需将调用的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,然后放置一个可设置的闭包,该闭包可以注册CellsFromxib(),而不仅仅是一个普通函数(使用为populateDate