PastaCoder
PastaCoder

Reputation: 1041

Selecting a TableViewCell provides nil for indexPathForSelectedRow()?.row in Swift

I have been trying to integrate a Pinboard bookmarks view (by parsing an RSS Feed and displaying it in a TableView) in my browser app, and I'm encountering a few issues, the main one is that selecting a cell always returns nil and causes a crash.

I followed this guide to create the view.

Loading the feed in the BlogReader complete project works perfectly, but using it on my own project always returns nil in indexPathForSelectedRow in the prepareForSegue function on the end.

Since my app is a browser, my Storyboard is set differently than the one on the BlogReader one. I have the main ViewController that includes a WKWebView and a UIButton, tapping the button performs a show segue to BookmarksViewController which has a container with an embed segue to BookmarksTableViewController - the TableView. BookmarksTableViewController has a show segue with the identifier "viewbookmark" to the main ViewController.

Here is the code I used:

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

What am I doing wrong?

Thanks,

PastaCoder

Upvotes: 0

Views: 1915

Answers (1)

Shuo
Shuo

Reputation: 8937

tableView.deselectRowAtIndexPath(indexPath, animated: true)

You just deselected in

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

Upvotes: 3

Related Questions