Ios 在Swift中拉动刷新

Ios 在Swift中拉动刷新,ios,swift,Ios,Swift,我正在尝试在我的表视图应用程序中实现拉入刷新。我一直在看人们的例子,我认为这就是它的要点: var refreshControl:UIRefreshControl! override func viewDidLoad() { super.viewDidLoad() self.refreshControl = UIRefreshControl() self.refreshControl.attributedTitle = NSAttributedString(str

我正在尝试在我的表视图应用程序中实现拉入刷新。我一直在看人们的例子,我认为这就是它的要点:

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()
就像我对其他变量做的一样,但我想不是。
你知道问题是什么吗

我收集您的类继承了
UITableViewController
UITableViewController
已经声明了
refreshControl
属性,如下所示:

@availability(iOS, introduced=6.0)
var refreshControl: UIRefreshControl?
你不需要覆盖它。只需去掉
var
声明并分配给继承的属性

由于继承的属性是可选的,因此需要使用
要将其展开:

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

只需在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!)

工作正常:)

非常感谢!我不知道refreshControl已经声明!我感谢你的帮助。但是现在,当我下拉刷新时,应用程序崩溃了:
终止为NSException类型的未捕获异常
try-catch应该去哪里?完整消息是什么?完整消息显示:
[SimpleText.TimelineTableViewController loadData:]:发送到实例0x16d5cfd0 2014-09-30 15:25:26.983 SimpleText[2639:472743]***由于未捕获异常“NSInvalidArgumentException”终止应用程序。
您没有发布任何发送
加载数据:
消息的代码,这与您的
refreshControl
编译器错误是一个单独的问题。很抱歉,这是我的错误,我将refresh的名称改为loadData:,我忘了提到这一点,哈哈