UITableView does not use the cell as a sender

I’m trying to segue and pass data (from UITableView to a UIViewController). My code is

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "IndividualAchievementsSegue" {
         let destination = segue.destinationViewController as? IndividualAchievementsViewController
        let cell = sender as! UITableViewCell
        let selectedRow = AchievementsTable.indexPathForCell(cell)!.row
         destination!.viaSegue = achievements[selectedRow]
        print(selectedRow)
    }
}

but the line let cell = sender as! UITableViewCell is throwing

Could not cast value of type 'ProgramName.AchievementsViewController' to 'UITableViewCell'

I have also tried

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "IndividualAchievementsSegue" {
         let destination = segue.destinationViewController as? IndividualAchievementsViewController
         let selectedRow = AchievementsTable.indexPathForSelectedRow!.row
         destination!.viaSegue = achievements[selectedRow]
        print(selectedRow)
    }
}

That fails because it finds nil unwrapping the optional during achievements[selectedRow] since that apparently gives me a nil from let selectedRow = AchievementsTable.indexPathForSelectedRow!.row

I think the problem is that your segue is connected with your AchievementsViewController to IndividualAchievementsViewController instead of UITableViewCell to IndividualAchievementsViewController . Check in the storyboard for that and correct the segue with TableCell to IndividualAchievementsViewController .

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » UITableView does not use the cell as a sender

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录