UIR刷新控件?没有名为 'endRefreshing' 的成员
UIRefreshControl? does not have a member named 'endRefreshing'
我正在尝试使用我的 UITableView 进行简单的拉动刷新,但出现错误:UIRefreshControl?没有名为 'endRefreshing' 的成员
我的 onRefresh 方法中 self.refreshControl.endRefreshing() 出现编译时错误
这是 viewController:
的代码
//
// ViewController.swift
// Rotten Tomatoes
//
// Created by Dhruv Mangtani on 2/7/15.
// Copyright (c) 2015 dhruv.mangtani. All rights reserved.
import UIKit
import SystemConfiguration
class ViewController: UITableViewController {
var moviesArray: NSArray?
var ratingsArray: NSArray?
override func viewDidLoad() {
super.viewDidLoad()
var refreshControl = UIRefreshControl()
refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: "onRefresh", forControlEvents: UIControlEvents.ValueChanged)
self.refreshControl = refreshControl
}
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
func onRefresh() {
delay(2, closure: {
self.refreshControl.endRefreshing()
})
}
override func viewDidAppear(animated:Bool){
super.viewDidAppear(animated)
let RottenTomatoesURLString = "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/top_rentals.json?apikey=rmvva9sq5s2tc2amv6db859f"
let request = NSMutableURLRequest(URL: NSURL(string: RottenTomatoesURLString)!)
if(isConnectedToNetwork()){
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler:{ (response, data, error) in
var errorValue: NSError? = nil
let dictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error:
&errorValue) as NSDictionary
self.moviesArray = dictionary["movies"] as? NSArray
self.tableView.reloadData()
})
}
else{
println("NO WIFI")
var networkLabel = UILabel()
networkLabel.text = "No Network Connection"
//self.addSubView
}
}
func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer([=11=])).takeRetainedValue()
}
var flags: SCNetworkReachabilityFlags = 0
if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
return false
}
let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection) ? true : false
}
}
}
由于 UITableView 的 refreshControl
属性 是可选的,因此您必须在使用前解包它。您可以使用 !
运算符解包,但更安全的方法是使用 ?
运算符解包,如果 refreshControl 为 nil,则不会导致运行时错误。
所以在你的 onRefresh 方法中改变...
self.refreshControl.endRefreshing()
到
self.refreshControl?.endRefreshing()
我正在尝试使用我的 UITableView 进行简单的拉动刷新,但出现错误:UIRefreshControl?没有名为 'endRefreshing' 的成员 我的 onRefresh 方法中 self.refreshControl.endRefreshing() 出现编译时错误 这是 viewController:
的代码//
// ViewController.swift
// Rotten Tomatoes
//
// Created by Dhruv Mangtani on 2/7/15.
// Copyright (c) 2015 dhruv.mangtani. All rights reserved.
import UIKit
import SystemConfiguration
class ViewController: UITableViewController {
var moviesArray: NSArray?
var ratingsArray: NSArray?
override func viewDidLoad() {
super.viewDidLoad()
var refreshControl = UIRefreshControl()
refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: "onRefresh", forControlEvents: UIControlEvents.ValueChanged)
self.refreshControl = refreshControl
}
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
func onRefresh() {
delay(2, closure: {
self.refreshControl.endRefreshing()
})
}
override func viewDidAppear(animated:Bool){
super.viewDidAppear(animated)
let RottenTomatoesURLString = "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/top_rentals.json?apikey=rmvva9sq5s2tc2amv6db859f"
let request = NSMutableURLRequest(URL: NSURL(string: RottenTomatoesURLString)!)
if(isConnectedToNetwork()){
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler:{ (response, data, error) in
var errorValue: NSError? = nil
let dictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error:
&errorValue) as NSDictionary
self.moviesArray = dictionary["movies"] as? NSArray
self.tableView.reloadData()
})
}
else{
println("NO WIFI")
var networkLabel = UILabel()
networkLabel.text = "No Network Connection"
//self.addSubView
}
}
func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer([=11=])).takeRetainedValue()
}
var flags: SCNetworkReachabilityFlags = 0
if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
return false
}
let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection) ? true : false
}
}
}
由于 UITableView 的 refreshControl
属性 是可选的,因此您必须在使用前解包它。您可以使用 !
运算符解包,但更安全的方法是使用 ?
运算符解包,如果 refreshControl 为 nil,则不会导致运行时错误。
所以在你的 onRefresh 方法中改变...
self.refreshControl.endRefreshing()
到
self.refreshControl?.endRefreshing()