2014-09-29 161 views
4

我正試圖在我的表格視圖應用程序中實現拉來刷新。我在人的例子已經環顧四周,我收集了,這是它非常要點:在Swift中進行刷新

var refreshControl:UIRefreshControl! 

override func viewDidLoad() 
{ 
    super.viewDidLoad() 

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

func refresh(sender:AnyObject) 
{ 
// Code to refresh table view 
} 

但是唯一的例子,我能看到的是從一段時間了,我知道的語言已經改變從那以後很多!當我嘗試使用上面的代碼,我收到以下錯誤緊挨着我refreshControl聲明:

Cannot override with a stored property 'refresh control' 

我首先想到之前讀其他的例子是,我必須聲明變量,像這樣:

var refreshControl:UIRefreshControl = UIRefreshControl() 

就像我做了一些其他變量,但我猜不是。 任何想法是什麼問題?

回答

4

我收集你的課程繼承UITableViewControllerUITableViewController已經聲明refreshControl屬性是這樣的:

@availability(iOS, introduced=6.0) 
var refreshControl: UIRefreshControl? 

你並不需要重寫它。只需擺脫您的var聲明並分配給繼承的屬性。

由於繼承財產Optional,你需要使用一個?!解開它:

refreshControl = UIRefreshControl() 
refreshControl!.attributedTitle = NSAttributedString(string: "Pull to refresh") 
refreshControl!.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged) 
tableView.addSubview(refreshControl!) 
+0

非常感謝你!我不知道refreshControl已經宣佈!我感謝您的幫助。但是現在,當我拉下來刷新時,應用程序崩潰: '終止於類型爲NSException的未捕獲異常,try catch應該放在哪裏? – 2014-09-29 22:18:59

+0

什麼是完整信息? – 2014-09-29 23:09:23

+0

完整的信息如下:'[SimpleText.TimelineTableViewController loadData:]:無法識別的選擇發送到實例0x16d5cfd0 2014-09-30 15:25:26.983 SimpleText [2639:472743] ***由於未捕獲的異常'NSInvalidArgumentException' ' – 2014-09-30 14:27:49

2

就在viewDidLoad中添加此代碼

self.refreshControl = UIRefreshControl() 
     self.refreshControl!.attributedTitle = NSAttributedString(string: "Pull to refresh") 
self.refreshControl!.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged) 
     self.tableView.addSubview(refreshControl!) 

工作得很好:)