【发布时间】:2020-09-22 18:18:54
【问题描述】:
概述
我正在尝试更好地了解扩展程序的工作原理。
在我的应用程序中,我有一个 ViewController。在那里我放了另一个类的视图。在这个自定义类中,我放置了一堆按钮和一个表格视图。我希望他们在我按下它们时在我的 tableView 中显示一些文本。
问题是我想编辑一些表格视图功能,以便更好地适应我的 ViewController。
我所知道的
我所知道的都是基于apple documentation
我在做什么
我应该说,我要做的是在将我的自定义类类型的对象添加到 ViewController 之后,向自定义视图的函数添加功能。
这是我的自定义类:
class CustomClass: UIView{
@IBOutlet weak var abtn: UIButton!
@IBOutlet weak var table: UITableView!
func setupTable(){
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
table.backgroundColor = UIColor.black.withAlphaComponent(0.1)
}
}
extension CustomClass: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("I want to add stuff here too")
}
//And more stuff that is not useful rn
}
在 ViewController 类中,我声明了一个 CustomClass 类型的变量。
@IBOutlet weak var custom: CustomClass!
在我看来DidLoad 我打电话给:custom.setupTable()
我需要做的是创建一个扩展来编辑属于custom 的tableview(我的ViewController 内部的CustomClass 类型的变量)。
我不知道该怎么做。
我知道如何使用扩展来扩展我的代码的功能,但我不知道如何使用它们来编辑这些其他功能。
问题
如何编辑属于自定义的tableview函数?
IE。如何更改行数或从调用对象的类更改单元格的布局?
我希望我已经足够清楚了......
【问题讨论】:
标签: ios swift class uitableview swift-extensions