问题描述
概述
我试图更好地了解扩展的工作方式。
在我的应用程序中,我有一个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!
在我的viewDidLoad中,我叫:custom.setupTable()
我需要做的是创建一个扩展来编辑属于custom
(属于ViewController内部的CustomClass类型的变量)的表视图。
我不知道该怎么做。
我知道如何与扩展一起使用以扩展代码的功能,但是我不知道如何使用它们来编辑其他功能。
问题
如何编辑属于定制的tableview函数?
就是如何更改行数或从调用对象的类中更改单元格的布局?
我希望我足够清楚...
解决方法
对于此特定示例...
将属性添加到您的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,numberOfRowsInSection section: Int) -> Int {
return theData.count
}
func tableView(_ tableView: UITableView,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
}
}
}