IOS8 TableViewController拉到刷新无法添加动作

时间:2014-06-17 13:03:00

标签: swift ios8

我想添加"拉动刷新"到桌子,但我不明白什么是错的。我添加了一个目标,但是当我使用它时没有任何事情发生

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var refresh = UIRefreshControl()
    refresh.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refresh.targetForAction("getServers", withSender: self)

    self.refreshControl = refresh
}

func getServers() {
        println("test")
}

UITableViewController

当我拉它时,没有任何事情发生

3 个答案:

答案 0 :(得分:10)

使用  refresh.addTarget(self, action: "getServers", forControlEvents:.ValueChanged)

而不是  refresh.targetForAction("getServers", withSender: self)

答案 1 :(得分:0)

@Yatheesha建议的AddTarget和关闭刷新控件使用getServers方法中的refresh.endRefreshing()。 发布整个代码

   override func viewDidLoad() {

   super.viewDidLoad()
   // Do any additional setup after loading the view, typically from a nib.

   let refresh = UIRefreshControl()
   refresh.attributedTitle = NSAttributedString(string: "Pull to refresh")
   refresh.addTarget(self,
            action:"refreshPulled",
            forControlEvents:.ValueChanged)
   self.refreshControl = refresh
   }

func refreshPulled() 
    {
    print("refreshPulled method")
    // call your reloadData() function here
    self.refreshControl!.endRefreshing()  // (note, needs unwrapper there)
    //to stop the refresh being displayed
    }

答案 2 :(得分:0)

我来到这里寻找如何使用网络上的数据刷新表格视图,以及这些答案的帮助,我自己的一些修补就是答案:

//make constant outdie viewDidLoad method
let refreshController = UIRefreshController()

override func viewDidLoad(){
  //
  // All your various other code
  //
  //refreshTableView() is a func you will create below your viewDidLoad method
  refreshController.addtarget(self, action: Selector(refreshTableView()), forControlEvents: .ValueChanged)

  //tableView is the name of your table view in your view.
  //refreshController is instances of the constant made outside of the viewDidload
  self.tableView.addSubview(refreshController)
}

func refreshTableView(){
  //this basically redownloads your parsed data/data from web
  self.tableView.reloadData()

  //this is why you declare refreshController outside view did load
  //endRefreshing kinda says what it does on the tin
  refreshController.endRefreshing()
}

这为您提供了在表视图上方的小旋转轮,然后在刷新数据时将其关闭。这是在我自己的应用程序中实现的,但不确定这可以实现多久。我的应用适用于iOS 8.1