【问题标题】:How do I use extensions to extend an object's tableview's functionality from the calling class?如何使用扩展从调用类扩展对象的 tableview 功能?
【发布时间】: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


    【解决方案1】:

    对于这个具体的例子...

    将属性添加到您的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
            }
        }
        
    }
    

    【讨论】:

    • 很好的解释,非常感谢!在我接受答案之前我还有一个问题:如果我想在我的表格视图中添加一个函数,所以我需要编辑 didSelectRowAt 函数怎么办?你会建议我怎么做?
    • @crost - 您需要提供比 “我需要编辑 didSelectRowAt 函数” 更详细的问题......这本身并不能任何意义。
    • 我在我的问题中编辑了扩展,最后一个函数也是我想编辑的。我的意思是我想知道如果我想我应该如何行动,例如,在该函数中添加一些东西以及从另一个视图控制器。让我知道我是否清楚
    • @crost - 您需要阅读协议/委托模式和闭包。我用一个使用闭包的例子编辑了我的答案。
    • 您非常友善和清晰,感谢您的时间和帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    相关资源
    最近更新 更多