Add Cell setup code to an StyledTableCell Extension (#797)

* Add Code Repository Cell extension file

* Move cell setup code to an extension of a StyledTableViewCell

* Add fatalError instead of force unwrap
This commit is contained in:
Nicholas Meschke
2017-11-04 14:55:05 +00:00
committed by Ryan Nystrom
parent 66481af678
commit 306eea70ec
5 changed files with 46 additions and 33 deletions

View File

@@ -99,28 +99,12 @@ final class RepositoryCodeDirectoryViewController: UIViewController, UITableView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? StyledTableCell else {
fatalError("Could not dequeueCell with identifier: \(cellIdentifier)")
}
let file = files[indexPath.row]
cell.textLabel?.text = file.name
cell.isAccessibilityElement = true
cell.accessibilityTraits |= UIAccessibilityTraitButton
let fileType = file.isDirectory
? NSLocalizedString("Directory", comment: "Used to specify the code cell is a directory.")
: NSLocalizedString("File", comment: "Used to specify the code cell is a file.")
cell.accessibilityLabel = cell.contentView.subviews
.flatMap { $0.accessibilityLabel }
.reduce("") { "\($0).\n\($1)" }
.appending(".\n\(fileType)")
cell.accessibilityHint = file.isDirectory
? NSLocalizedString("Shows the contents of the directory", comment: "")
: NSLocalizedString("Shows the contents of the file", comment: "")
let imageName = file.isDirectory ? "file-directory" : "file"
cell.imageView?.image = UIImage(named: imageName)?.withRenderingMode(.alwaysTemplate)
cell.imageView?.tintColor = Styles.Colors.blueGray.color
cell.accessoryType = file.isDirectory ? .disclosureIndicator : .none
cell.setup(with: file)
return cell
}