2014-09-29 162 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!) 

工作得很好:)