Reputation: 1259
So my segue is crashing here and I am not sure why...There are no errors shown but when I run the app in the simulator and then hit the button to segue it crashes with this error in the debugger: Could not cast value of type 'DNApp.StoriesTableViewController' (0x1077d5a80) to 'UITableViewCell' (0x109d8ca18). (lldb)
Below is the relevant code:
class StoriesTableViewController: UITableViewController, StoryTableViewCellDelegate {
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("StoryCell") as! StoryTableViewCell
let story = data[indexPath.row]
cell.configureWithStory(story)
cell.delegate = self
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("WebSegue", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// MARK: StoryTableViewCellDelegate
func storyTableViewCellDidTouchUpvote(cell: StoryTableViewCell, sender: AnyObject) {
// TODO: Implement Upvote
}
func storyTableViewCellDidTouchComment(cell: StoryTableViewCell, sender: AnyObject) {
performSegueWithIdentifier("CommentsSegue", sender: self)
}
//MARK: Misc
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "CommentsSegue" {
let toView = segue.destinationViewController as! CommentsTableViewController
let indexPath = tableView.indexPathForCell(sender as! UITableViewCell)!
toView.story = data[indexPath.row]
}
}
}
Upvotes: 1
Views: 905
Reputation: 14845
That is because your cell is not a UITableViewCell
but a StoryTableViewCell
. If you use if let
instead instead the forced wrapping you are seeing your app will not crash, however you won't be able to access the values in the cell, you just need to cast your cell correctly as below:
class StoriesTableViewController: UITableViewController, StoryTableViewCellDelegate {
var myIndexPat = NSIndexPath()
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
myIndexPath = indexPath
performSegueWithIdentifier("WebSegue", sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "CommentsSegue" {
let toView = segue.destinationViewController as! CommentsTableViewController
let indexPath = tableView.indexPathForCell(myIndexPath) as! StoryTableViewCell
toView.story = data[indexPath.row]
}
}
Upvotes: 1
Reputation: 1640
func storyTableViewCellDidTouchComment(cell: StoryTableViewCell, sender: AnyObject) {
performSegueWithIdentifier("CommentsSegue", sender: self)
}
In prepareForSegue method sender is kind of StoriesTableViewController class,force cast to UITableViewCell lead to crash.
Upvotes: 0
Reputation: 2782
Sorry, I misread your question entirely! Your crash is still due to force unwrapping. You should use optional bindings (if let
) instead. You can do multiple bindings at once and use a where
clause to make the code a little cleaner, too:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let toView = segue.destinationViewController as? CommentsTableViewController where segue.identifier == "CommentsSegue" {
if let cell = sender as? UITableViewCell,
let indexPath = tableView.indexPathForCell( cell ) {
toView.story = data[indexPath.row]
}
}
}
Upvotes: 1