如何使用扩展从调用 class 扩展对象的表视图的功能?
How do I use extensions to extend an object's tableview's functionality from the calling class?
概览
我正在尝试更好地了解扩展程序的工作原理。
在我的应用程序中,我有一个 ViewController。在那里我放了另一个 class 的视图。在这个自定义 class 中,我放置了一堆按钮和一个 table 视图。我希望它们在我按下它们时在我的 tableView 中显示一些文本。
问题是我想编辑一些 table 视图函数,以便更好地适应我的 ViewController.
我所知道的
我所知道的是基于 apple documentation
我在做什么
我应该说,我想做的是在将属于我的自定义 class 类型的对象添加到 ViewController.[=16= 之后向自定义视图的函数添加功能。 ]
这是我的习惯class:
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 class 中,我声明了一个 CustomClass 类型的变量。
@IBOutlet weak var custom: CustomClass!
在我的 viewDidLoad 中,我调用:
custom.setupTable()
我需要做的是创建一个扩展来编辑属于 custom
的 table 视图(我的 ViewController 中的 CustomClass 类型的变量)。
我不知道该怎么做。
我知道如何使用扩展来扩展我的代码的功能,但我不知道如何使用它们来编辑这些其他功能。
问题
如何编辑属于自定义的table视图函数?
IE。我如何才能更改行数或更改单元格的布局从 class 我调用对象?
希望我说得足够清楚...
对于这个具体的例子...
将 属性 添加到您的 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”中,您可以更改 属性:
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
}
}
不过,更有可能的是,您会为 table 设置/更改 data自定义 class.
这是一个示例,同时展示了如何使用 closure
来“回调”调用 class / 控制器:
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
}
}
}
概览
我正在尝试更好地了解扩展程序的工作原理。
在我的应用程序中,我有一个 ViewController。在那里我放了另一个 class 的视图。在这个自定义 class 中,我放置了一堆按钮和一个 table 视图。我希望它们在我按下它们时在我的 tableView 中显示一些文本。
问题是我想编辑一些 table 视图函数,以便更好地适应我的 ViewController.
我所知道的
我所知道的是基于 apple documentation
我在做什么
我应该说,我想做的是在将属于我的自定义 class 类型的对象添加到 ViewController.[=16= 之后向自定义视图的函数添加功能。 ]
这是我的习惯class:
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 class 中,我声明了一个 CustomClass 类型的变量。
@IBOutlet weak var custom: CustomClass!
在我的 viewDidLoad 中,我调用:
custom.setupTable()
我需要做的是创建一个扩展来编辑属于 custom
的 table 视图(我的 ViewController 中的 CustomClass 类型的变量)。
我不知道该怎么做。
我知道如何使用扩展来扩展我的代码的功能,但我不知道如何使用它们来编辑这些其他功能。
问题
如何编辑属于自定义的table视图函数?
IE。我如何才能更改行数或更改单元格的布局从 class 我调用对象?
希望我说得足够清楚...
对于这个具体的例子...
将 属性 添加到您的 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”中,您可以更改 属性:
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
}
}
不过,更有可能的是,您会为 table 设置/更改 data自定义 class.
这是一个示例,同时展示了如何使用 closure
来“回调”调用 class / 控制器:
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
}
}
}