I have this method which is used in a UIViewController base class that uses a Table View with dynamic prototypes and 1 prototype cell and right detail cell styling:
func tableView(_ homeworkTable: UITableView, didSelectRowAt indexPath: IndexPath) {
homeworkTable.deselectRow(at: indexPath, animated: true)
if let cell = homeworkTable.cellForRow(at: indexPath) {
if cell.detailTextLabel?.text == "" {
let homeworkWord = homeworkWords[indexPath.row]
if homeworkWord.isEmpty == false {
let split = homeworkWord.components(separatedBy: "::")
cell.detailTextLabel?.text = split[1]
}
} else {
cell.detailTextLabel?.text = ""
}
}
let now: TimeInterval = Date().timeIntervalSince1970
if (now - lastClick < 0.3) && (lastIndexPath?.row == indexPath.row ) {
self.scrollToTop()
}
lastClick = now
lastIndexPath = indexPath as NSIndexPath
}
I have another UIViewController that uses a custom table cell in an XIB file. It has a button and 2 UILabels. I got everything working for the button and the UILabel that occupies the first left hand side of the cell. However, the line that worked in the method above in the first view controller, when used in a didSelectRowAt in the view controller with a custom cell:
if let cell = tableView.cellForRow(at: indexPath)
doesn't seem to let me use something like:
cell.foreignWord.text = split[1]
for the custom cell, where foreignWord is the second UILabel in the custom cell.
I get the error:
UITabelViewCell has no member 'foreignWord'.
I have the nib registered to the tableView, and all the usual setup. I know the difference between the two viewControllers is that one is using cell prototypes and the other custom xib cells, so I'm thinking there's a different approach to instantiating a cell reference in a method that doesn't pass the cell parameter, so I can assign string text values to the UILabel.
cellForRow(at
returns the base class UITableViewCell
(the error clearly tells you that), with a custom cell you have to conditional downcast the type
if let cell = tableView.cellForRow(at: indexPath) as? MyCustomCell { ...