有没有一种方法可以通过其中一个单元格中的按钮刷新整个 UITableView?
is there a way of refreshing the whole UITableView through a button that is in one of the cells?
我有一个动态生成的 UITableView,其中有许多动态 UITableViewCells
和一个静态 UITableViewCell
。
静态的有一个按钮,我想在用户按下它时刷新整个 table 视图。
我附加到单元格的代码很简单:
class MyStaticCell: UITableViewCell {
@IBOutlet weak var sendCommentButton: UIButton!
@IBAction func sendCommentButtonAction(sender: AnyObject) {
//from here I want to refresh the table
}
}
如何从该按钮刷新父 table?在 class MyStaticCell
中我没有 table 的任何实例,所以这是我现在的问题 :|
您可以使用 superview 访问 tableView。
class MyStaticCell: UITableViewCell {
@IBOutlet weak var sendCommentButton: UIButton!
@IBAction func sendCommentButtonAction(sender: AnyObject) {
(superview as? UITableView)?.reloadData()
}
}
这并不像它应该的那样稳定,所以可以考虑这个扩展:
extension UIResponder {
func nextResponder<T: UIResponder>(ofType type: T.Type) -> T? {
switch nextResponder() {
case let responder as T:
return responder
case let .Some(responder):
return responder.nextResponder(ofType: type)
default:
return nil
}
}
}
它允许您找到特定类型的下一个父级,在单元格的情况下,UITableView
。
class MyStaticCell: UITableViewCell {
@IBOutlet weak var sendCommentButton: UIButton!
@IBAction func sendCommentButtonAction(sender: AnyObject) {
nextResponder(ofType: UITableView.self)?.reloadData()
}
}
最简洁的方法是通过委派。这确保单元格 class 不需要知道按下按钮时会发生什么;该逻辑可以保留在它所属的视图控制器中。
protocol CommentButtonProtocol {
func commentButtonTapped(sender: MyStaticCell)
}
class MyStaticCell: UITableViewCell {
@IBOutlet weak var sendCommentButton: UIButton!
var delegate: CommentButtonProtocol?
@IBAction func sendCommentButtonAction(sender: AnyObject) {
self.delegate?.commentButtonTapped(self)
}
}
然后在您的视图控制器中,您可以将其设置为 cellForRowAtIndexPath
中的委托并遵守协议以处理事件:
class ViewController: UIViewController, CommentButtonProtocol {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath) as! MyStaticCell
cell.delegate = self
return cell
}
func commentButtonTapped(sender: MyStaticCell) {
// Do whatever you need to do when the button is tapped
}
}
我有一个动态生成的 UITableView,其中有许多动态 UITableViewCells
和一个静态 UITableViewCell
。
静态的有一个按钮,我想在用户按下它时刷新整个 table 视图。
我附加到单元格的代码很简单:
class MyStaticCell: UITableViewCell {
@IBOutlet weak var sendCommentButton: UIButton!
@IBAction func sendCommentButtonAction(sender: AnyObject) {
//from here I want to refresh the table
}
}
如何从该按钮刷新父 table?在 class MyStaticCell
中我没有 table 的任何实例,所以这是我现在的问题 :|
您可以使用 superview 访问 tableView。
class MyStaticCell: UITableViewCell {
@IBOutlet weak var sendCommentButton: UIButton!
@IBAction func sendCommentButtonAction(sender: AnyObject) {
(superview as? UITableView)?.reloadData()
}
}
这并不像它应该的那样稳定,所以可以考虑这个扩展:
extension UIResponder {
func nextResponder<T: UIResponder>(ofType type: T.Type) -> T? {
switch nextResponder() {
case let responder as T:
return responder
case let .Some(responder):
return responder.nextResponder(ofType: type)
default:
return nil
}
}
}
它允许您找到特定类型的下一个父级,在单元格的情况下,UITableView
。
class MyStaticCell: UITableViewCell {
@IBOutlet weak var sendCommentButton: UIButton!
@IBAction func sendCommentButtonAction(sender: AnyObject) {
nextResponder(ofType: UITableView.self)?.reloadData()
}
}
最简洁的方法是通过委派。这确保单元格 class 不需要知道按下按钮时会发生什么;该逻辑可以保留在它所属的视图控制器中。
protocol CommentButtonProtocol {
func commentButtonTapped(sender: MyStaticCell)
}
class MyStaticCell: UITableViewCell {
@IBOutlet weak var sendCommentButton: UIButton!
var delegate: CommentButtonProtocol?
@IBAction func sendCommentButtonAction(sender: AnyObject) {
self.delegate?.commentButtonTapped(self)
}
}
然后在您的视图控制器中,您可以将其设置为 cellForRowAtIndexPath
中的委托并遵守协议以处理事件:
class ViewController: UIViewController, CommentButtonProtocol {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("staticCell", forIndexPath: indexPath) as! MyStaticCell
cell.delegate = self
return cell
}
func commentButtonTapped(sender: MyStaticCell) {
// Do whatever you need to do when the button is tapped
}
}