Swift 从自定义单元格重新加载tableview数据
我有一个自定义单元格的tableView 我还有一个.swift文件用于这个custrom单元 在这个文件中,我有一个函数,在输入参数时没有sender:AnyObject 如何从此函数调用tableView.reloadData()?尝试创建委托。(我假设你知道,如果你不看看苹果关于代理和协议的文档的话) 因此,我建议创建一个将在UITableViewController(或符合UITableViewDelegate协议的UIViewController)中实现的函数 首先,尝试在CustomCell.swift文件的顶部添加协议Swift 从自定义单元格重新加载tableview数据,swift,tableview,custom-cell,reloaddata,Swift,Tableview,Custom Cell,Reloaddata,我有一个自定义单元格的tableView 我还有一个.swift文件用于这个custrom单元 在这个文件中,我有一个函数,在输入参数时没有sender:AnyObject 如何从此函数调用tableView.reloadData()?尝试创建委托。(我假设你知道,如果你不看看苹果关于代理和协议的文档的话) 因此,我建议创建一个将在UITableViewController(或符合UITableViewDelegate协议的UIViewController)中实现的函数 首先,尝试在Custom
protocol CustomCellUpdater: class { // the name of the protocol you can put any
func updateTableView()
}
然后在CustomCell.swift中:
weak var delegate: CustomCellUpdater?
func yourFunctionWhichDoesNotHaveASender () {
...
delegate?.updateTableView()
}
之后,在您的UITableViewController
(或同等产品)中
最后,使您的UITableview
类符合CustomCellUpdater
协议
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourIdentifier", for: indexPath) as! YourTableViewCell
cell.delegate = self
}
从理论上讲,这应该是可行的。如果我遗漏了任何内容,请告诉我您是真的在寻找一个单元格来重新加载整个表,还是只是想重新绘制该单元格?如何“最后使UITableview类符合CustomCellUpdater协议”?:)类youuitableviewcontroller:UITableViewController,CustomCellUpdater{……。}这很遗憾,但什么都没发生。对了,我们忘了一件事。在类中的什么位置使用customCell?例如在CellForRowatineXpath中
the answer is simple you can use delegate and protocol to do this
first
go to the cell class
and above add this protocol
protocol updateCustomCell: class {
func updateTableView()
}
second
add this variable inside your cell class
weak var delegate: updateCustomCell?
third
go to the class u wanna update or access a variable their
and add this function there
func updateTableView() {
/* write what you want here
simply what this means is you are trying to say if something happed in the custom cell and you want to update something or even want to access something from the current class inside your customCell class use this function not the protocol function */
}
don't forget to implement the protocol inside the class (Very important)
the answer is simple you can use delegate and protocol to do this
first
go to the cell class
and above add this protocol
protocol updateCustomCell: class {
func updateTableView()
}
second
add this variable inside your cell class
weak var delegate: updateCustomCell?
third
go to the class u wanna update or access a variable their
and add this function there
func updateTableView() {
/* write what you want here
simply what this means is you are trying to say if something happed in the custom cell and you want to update something or even want to access something from the current class inside your customCell class use this function not the protocol function */
}
don't forget to implement the protocol inside the class (Very important)