Search code examples
swiftuitableviewxib

didSelectRowAt: IndexPath is not working and will not print to the console when the cell pressed on


I have an xib file which is a custom cell that is being loaded programmatically onto the tableview on the view controller. However, it never runs the function didSelectRowAt and print to the console.

Screen capture of the current code


Solution

  • Assuming functions like numberOfRows(in section:) and cellForRow(at indexPath:) are working, it seems that your UITableView's dataSource is set to ThumbnailViewModel, but not your UITableView's delegate property.

    Wherever you've set dataSource be sure to set your UITableView's delegate property to be the ThumbnailViewModel.

    Real cause: Try changing func tableView(_ tableView: UITableView, didSelectRowAt indexPath: NSIndexPath) to func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)