There are several errors :
1. you declare first
let cell: UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellID)!
then cell is of type UITableViewCell and not of the type of your subclass where you declared videoUrl
2. Then in the if you redeclare a new cell:
let cell: VideoTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellID) as! VideoTableViewCell
This cell is not modifying the one you defined at line 5, but creates a new one
Anyway, it only exist insit the if block.
3. cell cannot be nil :
- either the dequeue returns nil and then you will get a crash error
- otherwise cell is not nil
4. in the if block, you should create a new cell, not retry the same dequeue
So, you should write something like:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let filePath = try! FileManager.default.contentsOfDirectory(at: docDir, includingPropertiesForKeys: nil) //Returns array of documents files
let cellID = "cell"
var cell: VideoTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellID) as? VideoTableViewCell // as? may fail, so can be nil
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellID) as? VideoTableViewCell // I assume the style is default, you have to check
}
if cell == nil { return nil } // if everything before has failed !
cell!.videoUrl = (filePath[0]) //Using first part of array for testing. You have now to unwrap cell
return cell!
}