Skip to content

Instantly share code, notes, and snippets.

@Savchukv
Created May 31, 2017 14:22
Show Gist options
  • Save Savchukv/312f7f96427a728d31a8eb5643f26224 to your computer and use it in GitHub Desktop.
Save Savchukv/312f7f96427a728d31a8eb5643f26224 to your computer and use it in GitHub Desktop.
Example TableView Controller with extension UITableViewDataSource and UITableViewDelegate
//
// UserProfileViewController.swift
//
// Created by Vasiliy Savchuk on 01.02.17.
// Copyright © 2017 All rights reserved.
//
class UserProfileViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var userImages = ["", Constant.TimerImage, Constant.HelpImage, Constant.RateImage, Constant.AboutImage, Constant.ShareImage, Constant.RestoreImage, Constant.InfoImage]
var userLables = ["", Constant.TimerLabel, Constant.HelpLabel, Constant.RateLabel, Constant.AboutLabel, Constant.ShareLabel, Constant.RestoreLabel, Constant.InfoLabel]
//MARK: - Lifecicle Object
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: - Actions
@IBAction func userSettingsButtonDidPress(_ sender: Any) {
let storyboard = UIStoryboard.init(name: Constant.Storyboard.Main, bundle: nil)
let userSettingsVC = storyboard.instantiateViewController(withIdentifier: UserSettingsProfileViewController.className)
push(viewController: userSettingsVC, animated: true)
}
}
//MARK: - UITableViewDataSource
extension UserProfileViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userImages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: UserHeaderTableViewCell.className, for: indexPath as IndexPath) as! UserHeaderTableViewCell
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: UserTableViewCell.className, for: indexPath as IndexPath) as! UserTableViewCell
cell.tableInfoImage.image = UIImage(named: userImages[indexPath.row])
cell.tableInfoLabel.text = userLables[indexPath.row]
if indexPath.row == 1 {
cell.tablePremiumLabel.text = "Premium"
} else {
cell.tablePremiumLabel.text = ""
}
return cell
}
}
}
//MARK: - UITableViewDelegate
extension UserProfileViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 130
} else {
return 50
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment