无法在prepareforsegue上检索indexPath?

时间:2015-03-23 16:10:55

标签: swift

我在使用“comments”块检索prepareForSegue上的indexPath时遇到问题。注释按钮位于单元格内部,我从视图控制器创建了一个segue到所需的vc但是每次单击该按钮时,我在声明indexPath的部分得到“在展开可选值时意外发现nil”

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {

    if (identifier == "webPage1") {
        let indexPath: NSIndexPath = tableView.indexPathForSelectedRow()!
        if (arrayByVotes[indexPath.row].objectForKey("videoURL") as NSString == "")  {
            performSegueWithIdentifier("comments", sender: nil)
            return false
        }
    }
    return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "webPage1") {
        var urlToOpen:String = ""
        let indexPath: NSIndexPath = tableView.indexPathForSelectedRow()!

        if((arrayByVotes[indexPath.row].objectForKey("videoURL")) != nil) {
            urlToOpen = arrayByVotes[indexPath.row].objectForKey("videoURL") as String

            urlToOpen = urlToOpen.stringByReplacingOccurrencesOfString("http:", withString: "https:", options: .LiteralSearch, range: nil)


        }

        let sW: webpageViewController = segue.destinationViewController as webpageViewController
        sW.urlPath = urlToOpen
    } else if (segue.identifier == "comments") {
        let indexPath: NSIndexPath = tableView.indexPathForSelectedRow()!
        let sweet:PFObject = arrayByVotes[indexPath.row] as PFObject
        var postId = sweet.objectId

        let selectedFContent: String = arrayByVotes[indexPath.row].objectForKey("content") as String

        let fpvc: FeedPageViewController = segue.destinationViewController as FeedPageViewController

        fpvc.selectedFeedFeedContent = selectedFContent
        fpvc.ourpostId = postId
    }

}

然后我尝试将其包装在

if let indexPath = tableView.indexPathForSelectedRow()!

但是这不起作用,因为它不会每次都检索正确的indexPath。

本质上该程序应检查单元格是否包含URL。如果是,则使用webview调用vc,如果没有,则直接调用注释页面。

1 个答案:

答案 0 :(得分:0)

试试这个


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "webPage1") {

        let selectedRow = tableView.indexPathForSelectedRow()?.row
        var urlToOpen = arrayByVotes(selectedRow!) as? String 
        if((urlToOpen != nil) {
            urlToOpen = urlToOpen.stringByReplacingOccurrencesOfString("http:", withString: "https:", options: .LiteralSearch, range: nil)
        }

        let sW: webpageViewController = segue.destinationViewController as webpageViewController
        sW.urlPath = urlToOpen
    } else if (segue.identifier == "comments") {
        let selectedRow = tableView.indexPathForSelectedRow()?.row
        let sweet:PFObject = arrayByVotes(selectedRow!) as PFObject
        var postId = sweet.objectId

        let selectedFContent: String = arrayByVotes(selectedRow!).objectForKey("content") as String

        let fpvc: FeedPageViewController = segue.destinationViewController as FeedPageViewController

        fpvc.selectedFeedFeedContent = selectedFContent
        fpvc.ourpostId = postId
    }

}