【发布时间】: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