Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用扩展来扩展对象';s tableview';调用类的功能?_Ios_Swift_Class_Uitableview_Swift Extensions - Fatal编程技术网

Ios 如何使用扩展来扩展对象';s tableview';调用类的功能?

Ios 如何使用扩展来扩展对象';s tableview';调用类的功能?,ios,swift,class,uitableview,swift-extensions,Ios,Swift,Class,Uitableview,Swift Extensions,概述 我正在努力更好地理解扩展是如何工作的。 在我的应用程序中,我有一个ViewController。在那里我看到了另一个班级。在这个自定义类中,我放置了一组按钮和一个表视图。每当我按下它们时,我希望它们在我的tableView中显示一些文本。 问题是我想编辑一些表视图函数,以便更好地将其调整到我的ViewController 我所知道的 我所知道的都是基于 我在做什么 我想做的是,我应该说,在将自定义类类型的对象添加到ViewController之后,向自定义视图的函数添加功能 这是我的自定义

概述
我正在努力更好地理解扩展是如何工作的。
在我的应用程序中,我有一个ViewController。在那里我看到了另一个班级。在这个自定义类中,我放置了一组按钮和一个表视图。每当我按下它们时,我希望它们在我的tableView中显示一些文本。
问题是我想编辑一些表视图函数,以便更好地将其调整到我的ViewController

我所知道的
我所知道的都是基于

我在做什么
我想做的是,我应该说,在将自定义类类型的对象添加到ViewController之后,向自定义视图的函数添加功能

这是我的自定义类:

class自定义类:UIView{
@IB出口弱var abtn:UIButton!
@IBVAR表:UITableView!
func setupTable(){
table.delegate=self
table.dataSource=self
表.寄存器(UITableViewCell.self,强制重用标识符:“cellId”)
table.backgroundColor=UIColor.black.带AlphaComponent(0.1)
}
}
扩展CustomClass:UITableViewDelegate,UITableViewDataSource{
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回10
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=table.dequeueReusableCell(标识符为:“cellId”,表示:indexath)
返回单元
}
func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
打印(“我也想在这里添加内容”)
}
//还有更多无用的东西
}
在ViewController类中,我声明了CustomClass类型的变量

@ibvar-custom:CustomClass
在我的视图中,我调用:
custom.setupTable()

我需要做的是创建一个扩展来编辑属于
custom
(ViewController中的CustomClass类型变量)的tableview

我不知道该怎么做

我知道如何使用扩展来扩展代码的功能,但我不知道如何使用它们来编辑这些其他功能

问题
如何编辑属于自定义的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
    }
    
}
然后,在“调用类”中,可以更改该属性:

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
    }
    
}
不过,更可能的情况是,您正在为自定义类中的表设置/更改数据

下面是一个示例,并展示了如何使用
闭包
来“回调”调用类/控制器:

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函数”更详细的问题。。。这本身没有任何意义。我在问题中编辑了扩展名,最后一个函数也是我想编辑的。我的意思是,我想知道如果我想,我应该如何行动,例如,在该函数中添加一些东西,以及从其他视图控制器。如果我是,请告诉我clear@crost-您需要阅读协议/委托模式和闭包。我用一个使用闭包的例子编辑了我的答案。你非常友好和清楚,谢谢你的时间和帮助。