概述
我正在努力更好地了解扩展是如何工作的
在我的应用程序中,我有一个ViewController。在那里我看到了另一个班级。在这个自定义类中,我放置了一堆按钮和一个表视图。每当我按下它们时,我希望它们在我的表中显示一些文本View
问题是我想编辑一些表视图函数,以便更好地将其调整到我的ViewController。
我所知道的
我所了解的都是基于苹果文档
我正在做的
我想说的是,在将属于我的自定义类类型的对象添加到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
(ViewController中的CustomClass类型的变量(的表视图。
我不知道该怎么做。
我知道如何使用扩展来扩展代码的功能,但我不知道如何使用它们来编辑这些其他函数。
问题
如何编辑属于自定义的表视图函数
Ie。我怎样才能从调用对象的类中更改行数或更改单元格的布局?
我希望我足够清楚。。。
对于这个特定的例子。。。
将属性添加到CustomClass
:
class CustomClass: UIView {
// this may be changed by the "calling class"
var numRows: Int = 10
@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 {
// don't make this a hard-coded number
//return 10
return numRows
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
return cell
}
//And more stuff that is not useful rn
}
然后,在你的";调用类";,您可以更改该属性:
class ExampleViewController: UIViewController {
let myView = CustomClass()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myView)
// constraints, etc
// change the number of rows in the table in myView
myView.numRows = 20
}
}
不过,更有可能的是,您要为自定义类中的表设置/更改数据。
这里有一个例子,同时展示了如何使用closure
来";回叫";到主叫类/控制器:
class CustomClass: UIView {
// this may be changed by the "calling class"
var theData: [String] = []
// closure to "call back" to the controller
var callback: ((IndexPath) -> ())?
@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 theData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
cell.textLabel?.text = theData[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// tell the controller the cell was selected
callback?(indexPath)
}
}
class ExampleViewController: UIViewController {
let myView = CustomClass()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myView)
// constraints, etc
// set the data in CustomClass
myView.theData = [
"First row",
"Second row",
"Third",
"Fourth",
"etc..."
]
myView.callback = { indexPath in
print("CustomClass myView told me (indexPath) was selected!")
// do what you want
}
}
}