[英]Selecting a TableViewCell provides nil for indexPathForSelectedRow()?.row in Swift
我一直在尝试在浏览器应用程序中集成Pinboard书签视图(通过解析RSS Feed并将其显示在TableView中),但是遇到了一些问题,主要的问题是选择单元格总是返回nil并导致崩溃
我按照本指南创建了视图。
将Feed加载到BlogReader完整项目中的工作原理完美,但是在我自己的项目中使用该Feed总是最后在prepareForSegue函数的indexPathForSelectedRow中返回nil。
由于我的应用是浏览器,因此我的Storyboard的设置与BlogReader上的设置不同。 我有一个包含WKWebView和UIButton的主ViewController,点击该按钮可对BookmarksViewController进行显示,其中有一个容器,其中包含一个嵌入BookmarksTableViewController的容器-TableView。 BookmarksTableViewController在主ViewController上有一个显示标识符为“ viewbookmark”的节目。
这是我使用的代码:
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
tableView.deselectRowAtIndexPath(indexPath, animated: true)
您刚刚取消选择
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.