如何使用扩展从调用类扩展对象的表视图功能?

如何解决如何使用扩展从调用类扩展对象的表视图功能?

概述
我试图更好地了解扩展的工作方式。
在我的应用程序中,我有一个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
        }
    }
    
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?