Skip to content

Instantly share code, notes, and snippets.

@RNHTTR
Created September 25, 2017 02:53
Show Gist options
  • Save RNHTTR/ac6731c6dbfd1794b79ba5597c90d964 to your computer and use it in GitHub Desktop.
Save RNHTTR/ac6731c6dbfd1794b79ba5597c90d964 to your computer and use it in GitHub Desktop.
Customize the cells in a UITableView
// Use tableView(_:cellForRowAt:) to customize the cells in a table view.
// This example demonstrates an instance of how one of this app's table views uses this method.
// Create some sort of data source. This is often an array or, in this case, a dictionary.
var dataSource: [String: String] {
return ["cellForRowAt: indexPath": "Update cell information based on the data source\nReturns a UITableViewCell.",
"numberOfRowsInSection: Int": "Determine the number of rows needed for each section of the tableView\nReturns an Int",
"titleForHeaderInSection: Int": "Set the title for a particular section of your tableView\nReturns a String"]
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// This table view uses a custom cell, DataSourceTableViewCell, that was created in a separate file. This cell has two
// UILabels, a title label and a detail label.
let cell = tableView.dequeueReusableCell(withIdentifier: "DataSourceTableViewCell") as! DataSourceTableViewCell
// Get the titles and details for their respective labels in an array.
var titles = Array(dataSource.keys)
var details = Array(dataSource.values)
// Access the title based on the indexPath's row property and set it to the title label's text property.
// Set the color of the label to green.
cell.title.text = titles[indexPath.row]
cell.title.textColor = UIColor.green
// Access the title based on the indexPath's row property and set it to the detail label's text property.
// Set the color of the label to green.
cell.detail.text = details[indexPath.row]
cell.detail.textColor = UIColor.green
return cell
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment