我一直在尝试在我的浏览器应用中集成Pinboard书签视图(通过解析RSS Feed并在TableView中显示),我遇到了一些问题,主要是选择一个单元格总是返回nil并导致崩溃。
我按照this guide创建了视图。
在BlogReader完整项目中加载Feed非常有效,但在我自己的项目中使用它总是在最后的prepareForSegue函数中的indexPathForSelectedRow中返回nil。
由于我的应用是浏览器,因此我的故事板设置与BlogReader上的设置不同。我有一个主要的ViewController,包括一个WKWebView和一个UIButton,点击按钮执行show segue到BookmarksViewController,它有一个容器,带有一个嵌入到BookmarksTableViewController的segue - TableView。 BookmarksTableViewController有一个show segue,标识符为" viewbookmark"到主ViewController。
以下是我使用的代码:
import UIKit
class BookmarksTableViewController: UITableViewController, NSXMLParserDelegate {
var parser: NSXMLParser = NSXMLParser()
var bookmarks: [Bookmark] = []
var bookmarkTitle: String = String()
var bookmarkLink: String = String()
var eName: String = String()
override func viewDidLoad() {
super.viewDidLoad()
let url:NSURL = NSURL(string: "http://feeds.pinboard.in/rss/secret:XXX/u:XXXX/")!
parser = NSXMLParser(contentsOfURL: url)!
parser.delegate = self
parser.parse()
}
// MARK: - NSXMLParserDelegate methods
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
eName = elementName
if elementName == "item" {
bookmarkTitle = String()
bookmarkLink = String()
}
}
func parser(parser: NSXMLParser, foundCharacters string: String?) {
let data = string!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
if (!data.isEmpty) {
if eName == "title" {
bookmarkTitle += data
} else if eName == "link" {
bookmarkLink += data
}
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "item" {
let bookmarkItem: Bookmark = Bookmark()
bookmarkItem.bookmarkTitle = bookmarkTitle
bookmarkItem.bookmarkLink = bookmarkLink
bookmarks.append(bookmarkItem)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bookmarks.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
let bookmarkItem: Bookmark = bookmarks[indexPath.row]
cell.textLabel?.text = bookmarkItem.bookmarkTitle
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50.0
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
// get to the next screen
self.performSegueWithIdentifier("viewbookmark", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "viewbookmark" {
let selectedRow = tableView.indexPathForSelectedRow()?.row
let bookmarkItem: Bookmark = bookmarks[selectedRow!]
let viewController = segue.destinationViewController as! ViewController
viewController._bookmarkLink = bookmarkItem.bookmarkLink
let url: NSURL = NSURL(string: bookmarkItem.bookmarkLink)!
let request: NSURLRequest = NSURLRequest(URL: url)
viewController._webView!.loadRequest(request)
}
}
}
我做错了什么?
谢谢,
PastaCoder
答案 0 :(得分:3)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
您刚刚在
中取消选择override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {