【问题标题】:is there a way of refreshing the whole UITableView through a button that is in one of the cells?有没有办法通过一个单元格中的按钮刷新整个 UITableView?
【发布时间】:2016-09-24 00:25:32
【问题描述】:

我有一个动态生成的 UITableView,其中包含许多动态 UITableViewCells 和一个静态 UITableViewCell

静态的有一个按钮,我想在用户按下它时刷新整个表格视图。

我附加到单元格的代码很简单:

class MyStaticCell: UITableViewCell {

    @IBOutlet weak var sendCommentButton: UIButton!

    @IBAction func sendCommentButtonAction(sender: AnyObject) {

        //from here I want to refresh the table

    }
}

如何通过该按钮刷新父表?在MyStaticCell 类中,我没有任何表实例,所以这是我现在的问题:|

【问题讨论】:

    标签: ios swift uitableview swift-protocols


    【解决方案1】:

    最简洁的方法是通过委托。这确保了单元类不需要知道按下按钮时应该发生什么;该逻辑可以保留在它所属的视图控制器中。

    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
        }
    
    } 
    

    【讨论】:

    • 阅读您的帖子是我发现的关于 Swift 协议和 POP 思维的最有见地的资源。谢谢!
    【解决方案2】:

    您可以使用 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()
        }
    }
    

    【讨论】:

    • 感谢您的澄清。我刚刚意识到刷新表格对我来说是不够的,因为我需要从服务器获取新数据然后刷新表格......我已经在我的父 UITableViewController 中有方法,所以我可以在按钮上调用它们按。有没有办法在从单元格内访问父组件时调用该方法?
    • 是的,只需使用nextResponderOfType(ParentViewController.self)?.parentMethod()
    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    相关资源
    最近更新 更多