我正在考虑检查互联网连接并显示一条消息,如果这里没有连接,所以现在我有一个TableView,我想我必须删除或替换这个表视图或只显示一个单元格并显示一条消息比如“没有互联网连接”,所以首先我添加了一个文件来检查连接,所以我这样做了:
override func viewDidLoad()
{
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor(red: 38.0/255.0, green: 51.0/255.0, blue: 85.0/255.0, alpha: 1.0)
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Gotham", size: 13)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
self.title = "ACTUALITÉS"
if Reachability.isConnectedToNetwork() == true {
self.beginParsing()
} else {
//Here I'm gonna do something and display an image
}
}
@IBAction func menuTapped(sender: AnyObject) {
print(delegate)
delegate?.toggleLeftPanel?()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func beginParsing()
{
posts = []
parser = NSXMLParser(contentsOfURL:(NSURL(string:"url..."))!)!
parser.delegate = self
parser.parse()
tableView!.reloadData()
}
//XMLParser Methods
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
{
element = elementName
if (elementName as NSString).isEqualToString("item")
{
elements = NSMutableDictionary()
elements = [:]
title1 = NSMutableString()
title1 = ""
date = NSMutableString()
date = ""
dscrptn = NSMutableString()
dscrptn = ""
url = NSURL()
urlString = NSMutableString()
urlString = ""
} else {
title1 = NSMutableString()
title1 = "No connection"
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
if (elementName as NSString).isEqualToString("item") {
if !title1.isEqual(nil) {
elements.setObject(title1, forKey: "title")
}
if !date.isEqual(nil) {
elements.setObject(date, forKey: "date")
}
if !dscrptn.isEqual(nil) {
elements.setObject(dscrptn, forKey: "dscrptn")
}
if !urlString.isEqual(nil) {
elements.setObject(urlString, forKey: "urlString")
}
posts.addObject(elements)
}
}
func parser(parser: NSXMLParser, foundCharacters string: String)
{
if element.isEqualToString("title") {
title1.appendString(string)
} else if element.isEqualToString("pubDate") {
date.appendString(string)
} else if element.isEqualToString("description") {
dscrptn.appendString(string)
} else if element.isEqualToString("link") {
urlString.appendString(string)
}
}
//Tableview Methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return posts.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let trimUrl = posts.objectAtIndex(indexPath.row).valueForKey("urlString") as! String
UIApplication.sharedApplication().openURL(NSURL(string: trimUrl.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()))!)
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
return basicCellAtIndexPath(indexPath)
}
func basicCellAtIndexPath(indexPath:NSIndexPath) -> ActuTblCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ActuTblCell
setTitleForCell(cell, indexPath: indexPath)
setDateForCell(cell, indexPath: indexPath)
setDescriptionForCell(cell, indexPath: indexPath)
return cell
}
func setTitleForCell(cell:ActuTblCell, indexPath:NSIndexPath) {
cell.titleActuCell?.text = posts.objectAtIndex(indexPath.row).valueForKey("title") as! NSString as String
}
func setDateForCell(cell:ActuTblCell, indexPath:NSIndexPath) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let dateString = posts.objectAtIndex(indexPath.row).valueForKey("date") as! NSString as String
if let dateAdded = dateFormatter.dateFromString(dateString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()))
{
dateFormatter.dateFormat = "dd/MM/yyyy"
cell.dateActuCell?.text = "\(dateFormatter.stringFromDate(dateAdded))"
}
//cell.dateActuCell?.text = posts.objectAtIndex(indexPath.row).valueForKey("date") as! NSString as String
}
func setDescriptionForCell(cell:ActuTblCell, indexPath:NSIndexPath) {
cell.descriptionActuCell?.text = (posts.objectAtIndex(indexPath.row).valueForKey("dscrptn") as! NSString as String).stripHTML()
}
}
let htmlReplaceString : String = "<[^>]+>"
extension NSString {
func stripHTML() -> NSString {
return self.stringByReplacingOccurrencesOfString(htmlReplaceString, withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range: NSRange(location: 0,length: self.length)) as NSString
}
}
extension String {
func stripHTML() -> String {
return self.stringByReplacingOccurrencesOfString(htmlReplaceString, withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range: nil)
}
}
我知道如何检查连接,但在这里我不知道什么是更好的解决方案以及如何做到这一点。
答案 0 :(得分:0)
您想要达到的目标称为空状态。可以找到空状态的想法www.pttrns.com。在您的情况下,没有网络连接时显示空状态。另一种情况可能是网络连接可用,但 post 数组在解析内容(XML / JSON)后没有项目。
我建议你使用第三方代码:
另一种方法是将背景图像设置为tableView。我想要注意背景图像应该是设备屏幕的大小。你可以这样做:
let tempImageView = UIImageView(image: UIImage(named: "yourImage"))
tempImageView.frame = self.tableView.frame
self.tableView.backgroundView = tempImageView;