Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active February 12, 2017 09:41
Show Gist options
  • Save KentarouKanno/75a39ae85f364779d9b3 to your computer and use it in GitHub Desktop.
Save KentarouKanno/75a39ae85f364779d9b3 to your computer and use it in GitHub Desktop.
UITableView Section Template

UITableView Section Template

★ Template - 1

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    let sectionTitleArray = ["Alphabet Num","Number","Alphabet"," "]
    
    // Data Array
    var dataArray1 = ["One","Two","Three","Four","Five"]
    var dataArray2 = ["1","2","3"]
    var dataArray3 = ["a","b","c","d"]
    var dataArray4 = ["A","B","C","D","E"]
    var dataArrayGroup: [[String]] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Create Data
        dataArrayGroup = [dataArray1, dataArray2, dataArray3, dataArray4]
        
        tableView.tableFooterView = UIView()
        tableView.estimatedRowHeight = 20
        tableView.rowHeight = UITableViewAutomaticDimension
    }
    
    // MARK: - TableView Delegate & DataSource
    
    // Section Title
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sectionTitleArray[section]
    }
    
    // Section Count
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArrayGroup.count
    }
    
    // Row Count
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArrayGroup[section].count
    }
    
    // Generate Cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = dataArrayGroup[indexPath.section][indexPath.row]
        return cell
    }
    
    // Select Cell
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

★ Template - 2

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    typealias sectionType = [(HeaderVew: UIView, height: CGFloat)]
    
    @IBOutlet weak var tableView: UITableView!
    let sectionTitleArray = ["Alphabet Num","Number","Alphabet"," "]
    
    // Data Array
    var dataArray1 = ["One","Two","Three","Four","Five"]
    var dataArray2 = ["1","2","3"]
    var dataArray3 = ["a","b","c","d"]
    var dataArray4 = ["A","B","C","D","E"]
    var dataArrayGroup: [[String]] = []
    
    var sectionItemArray: [(HeaderVew: UIView, height: CGFloat)] = Array()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Create Data
        dataArrayGroup = [dataArray1, dataArray2, dataArray3, dataArray4]
        
        // Create Section Header Data
        sectionItemArray = self.generateSectionHeader(titleArray: sectionTitleArray, parentView: self.view)
        
        tableView.tableFooterView = UIView()
        tableView.estimatedRowHeight = 20
        tableView.rowHeight = UITableViewAutomaticDimension
    }
    
    // Generate Section Header Data
    func generateSectionHeader(titleArray: [String], parentView: UIView) -> sectionType {
        var sectionHeaderArray = sectionType()
        for sectionTitle in titleArray {
            if sectionTitle.isEmpty {
                
                // No Section Header
                sectionHeaderArray.append((HeaderVew: UIView(),height: 0))
            } else {
                
                // Make Section Header
                let sectionBaseView = UIView(frame: CGRect(x: 0, y: 0, width: parentView.frame.size.width, height: 25))
                sectionBaseView.backgroundColor = UIColor(red: 0.8514, green: 0.8514, blue: 0.8514, alpha: 1.0)
                let headerLabel = UILabel(frame: CGRect(x: 10, y: 0, width: parentView.frame.size.width - 20, height: 25))
                headerLabel.text = sectionTitle
                headerLabel.font = UIFont.boldSystemFont(ofSize: 12)
                sectionBaseView.addSubview(headerLabel)
                sectionHeaderArray.append((HeaderVew: sectionBaseView,height: 25))
            }
        }
        return sectionHeaderArray
    }
    
    // MARK: - TableView Delegate & DataSource
    
    // Section Header View
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return sectionItemArray[section].HeaderVew
    }
    
    // Section Header Height
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return sectionItemArray[section].height
    }
    
    // Section Count
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArrayGroup.count
    }
    
    // Row Count
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArrayGroup[section].count
    }
    
    // Generate Cell
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = dataArrayGroup[indexPath.section][indexPath.row]
        return cell
    }
    
    // Select Cell
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

Table Section Image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment