Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TableViewController: UITableViewController {
- let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
- // cell reuse id (cells that scroll out of view can be reused)
- let cellReuseIdentifier = "cell"
- // don't forget to hook this up from the storyboard
- @IBOutlet var personList: UITableView!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Register the table view cell class and its reuse id
- self.personList.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
- // (optional) include this line if you want to remove the extra empty cell divider lines
- // self.tableView.tableFooterView = UIView()
- // This view controller itself will provide the delegate methods and row data for the table view.
- tableView.delegate = self
- tableView.dataSource = self
- }
- // number of rows in table view
- func personList(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return self.animals.count
- }
- // create a cell for each table view row
- func personList(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- // create a new cell if needed or reuse an old one
- let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
- // set the text from the data model
- cell.textLabel?.text = self.animals[indexPath.row]
- return cell
- }
- // method to run when table view cell is tapped
- func personList(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- print("You tapped cell number \(indexPath.row).")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement