UIRefreshControl?没有名为“endRefreshing”的成员
Posted
技术标签:
【中文标题】UIRefreshControl?没有名为“endRefreshing”的成员【英文标题】:UIRefreshControl? does not have a member named 'endRefreshing' 【发布时间】:2015-02-14 23:33:35 【问题描述】:我正在尝试使用我的 UITableView 进行简单的拉动刷新,但出现错误: 我的 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($0)).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
【问题讨论】:
【参考方案1】:由于 UITableView 的 refreshControl
属性是可选的,因此您必须在使用前解开它。您可以使用 !
运算符展开,但更安全的方法是使用 ?
运算符展开,如果 refreshControl 为 nil,则不会导致运行时错误。
所以在你的 onRefresh 方法中改变...
self.refreshControl.endRefreshing()
到
self.refreshControl?.endRefreshing()
【讨论】:
我做了然后它给出了一个错误,说无法将表达式的类型'(IntegerLiteralConvertible,closure: () -> () -> $T3)' 转换为类型'Void' 似乎是一个编译器错误。如果您在该行上方添加任何内容,例如let a = 1
或println("test")
,错误就会消失。很奇怪。以上是关于UIRefreshControl?没有名为“endRefreshing”的成员的主要内容,如果未能解决你的问题,请参考以下文章
为啥自定义 UIRefreshControl 类的背景颜色在 Swift 中没有改变?
没有 tableView 的 UIRefreshControl
将 UIRefreshControl 添加到 UIScrollView
没有 UITableViewController 的 UIRefreshControl 不调用选择器