gpt4 book ai didi

ios - 在 TableView 单元格选择上打开 URL(RSS 阅读器)

转载 作者:行者123 更新时间:2023-11-30 13:57:08 24 4
gpt4 key购买 nike

我有一个带有表格 View 的场景:

enter image description here

这里我做了一个 RSS 阅读器,对于每个元素我都得到了:标题、日期、描述和我的 url。在“ TableView 单元格”上,单击“我想打开浏览器/导航器”并传递新单击的 URL。

我这样做了:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let myWebView = self.storyboard!.instantiateViewControllerWithIdentifier("ActualitesViewController") as! ActualitesViewController

myWebView.url = NSURL(string: posts.objectAtIndex(indexPath.row).valueForKey("url"))

self.presentViewController(myWebView, animated: true, completion: nil)


}

但我收到此错误:

/Users/.../ActualitesViewController.swift:144:25: Cannot invoke initializer for type 'NSURL' with an argument list of type '(string: AnyObject?)'

完整代码:

import UIKit

@objc
protocol ActualitesViewControllerDelegate {
optional func toggleLeftPanel()
optional func collapseSidePanels()
}

class ActualitesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSXMLParserDelegate {
@IBOutlet var tableView: UITableView!

var parser = NSXMLParser()
var posts = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
var title1 = NSMutableString()
var date = NSMutableString()
var dscrptn = NSMutableString()
var url = NSMutableString()

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"

self.beginParsing()

}

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 = NSMutableString()
url = ""
}
}

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 !url.isEqual(nil) {
elements.setObject(url, forKey: "url")
}

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") {
url.appendString(string)
}
}

//Tableview Methods

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return posts.count
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let myWebView = self.storyboard!.instantiateViewControllerWithIdentifier("ActualitesViewController") as! ActualitesViewController

myWebView.url = NSURL(string: posts.objectAtIndex(indexPath.row).valueForKey("url"))

self.presentViewController(myWebView, animated: true, completion: nil)


}

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)
}
}

编辑

变化:

var url = NSURL()
var urlString = NSMutableString()


//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 = ""
}
}

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)
}
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

let myWebView = self.storyboard!.instantiateViewControllerWithIdentifier("ActualitesViewController") as! ActualitesViewController
print(urlString)

myWebView.url = NSURL(string: posts.objectAtIndex(indexPath.row).valueForKey("urlString") as! String)!

self.presentViewController(myWebView, animated: true, completion: nil)


}

urlString 打印正确,但我在这一行收到错误:

myWebView.url = NSURL(string: posts.objectAtIndex(indexPath.row).valueForKey("urlString") as! String)!

错误:

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)

在浏览器中打开:

let trimUrl = posts.objectAtIndex(indexPath.row).valueForKey("urlString") as! String

UIApplication.sharedApplication().openURL(NSURL(string: trimUrl.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()))!)

最佳答案

valueForKey 返回 AnyObject?,它是可选的任何对象。如果您确定在这种情况下它实际上会返回一个String,那么您可以强制将其向下转换为String

所以

myWebView.url = NSURL(string: posts.objectAtIndex(indexPath.row).valueForKey("url"))

成为

myWebView.url = NSURL(string: posts.objectAtIndex(indexPath.row).valueForKey("url") as! String)

尽管更好的方法是创建一个模型对象,该对象具有诸如 titleurl 等属性。然后创建 posts 这些类型的模型对象的数组 posts: [Model] 然后在调用 objectAtIndex 之后,您和编译器都会知道您正在得到返回一个 Model 对象,此时您可以直接调用 titleurl 等内容,而无需使用 valueForKey 和迫使沮丧。

关于ios - 在 TableView 单元格选择上打开 URL(RSS 阅读器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33478979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com