Property in Swift - Computed property | Property Observers (WillSet & DidSet)

   Computed property -  Computed properties are part of a property type in Swift.  Stored properties are the most common which save and return a stored value  Computed properties calculate (rather than store) a value. A computed property provides a getter and an optional setter to indirectly access other properties and values. Computed property in swift is an interesting topic as you can write code inside a property in swift. Computed properties are provided by classes, structures, and enumerations but stored properties are provided only by classes and structures. This computed property swift tutorial has some tips on how to use the computed property in the right way and avoid the common mistakes that swift programming beginners do while computed property. Example :- Computed property A Calculate Simple Intrest struct CalculateLoan {      var amount : Int      var rate : Int      var years : Int      var simpleInterest: Int {          get {              return ( amount * rate

How to Use Multiple Sections in UITableView iOS Swift !

Multiple sections in UITableView iOS Swift.

UITableView is very important part of iOS ecosystem. So we split tableviews in sections. Then its easier to find right information. 


Multiple Sections in UITableView iOS Swift 5


1. First let’s create a project as usual. Create a new single view application X code project. Set project name to UIViewController. 

2. Go to main storyboard & select view controller & use UITableView

3. Select tableview & make it initial view controller 

4 Create a custom Sections Class like Name => TableSections, create register cell static return getCellNibsmethod. Then create  4 section enum “TableItems” then after append all sections to an array model.


import UIKit

struct CellNib{

    static func getCellNibs() -> [String] {

        return ["Cell1","Cell2","Cell3","Cell4"]

    }

}

enum TableItems: Int {

    case TableSections1

    case TableSections2

    case TableSections3

    case TableSections4

}

class TableSections: NSObject {

    var sectionType : TableItems?

    init(tItems:TableItems) {

        sectionType = tItems

    }

class func sectionsForTable()->[TableSections] {

        var sections = [TableSections]()

        sections.append(TableSections(tItems: TableItems.TableSections1))

        sections.append(TableSections(tItems: TableItems.TableSections2))

        sections.append(TableSections(tItems: TableItems.TableSections3))

        sections.append(TableSections(tItems: TableItems.TableSections4))

        return sections

}

}

5. After Create a custom 4 UITableViewCell with xib. Cell1, Cell2, Cell3, Cell4.


import UIKit

class Cell1: UITableViewCell {

    override func awakeFromNib() {

        super.awakeFromNib()

        // Initialization code

    }

    override func setSelected(_ selected: Bool, animated: Bool) {

        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state

    } 

}

Same Cell2, Cell3, Cell4

6. After Create a TableCommonVC UIViewController and UITableViewDelegate, UITableViewDataSource, setup section, and register table cells.


import UIKit

class TableCommonVC: UIViewController, UITableViewDelegate, UITableViewDataSource{

    var tSections: [TableSections]?

    @IBOutlet weak var tlbView : UITableView!

    override func viewDidLoad() {

        super.viewDidLoad()

        self.setupSection()

        self.setTlbNibs()

        self.tlbView.reloadData()

    }

    func setTlbNibs(){

        for item in CellNib.getCellNibs() {

            let nib = UINib(nibName: item , bundle: nil)

            self.tlbView.register(nib, forCellReuseIdentifier: item)

        }

    }

    func setupSection(){

        self.tSections = TableSections.sectionsForTable()

    }

    //MARK: UITableView Delegate & DataSource

    func numberOfSections(in tableView: UITableView) -> Int {

        return (self.tSections != nil) ? self.tSections!.count:0

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return setRows(tItems: self.tSections![section].sectionType!)

    }

    func setRows(tItems: TableItems)->Int {

        switch tItems {

            case .TableSections1:return getS1RowCount()

            case .TableSections2:return getS2RowCount()

            case .TableSections3:return getS3RowCount()

            case .TableSections4:return getS4RowCount()

        }

    }

    func getS1RowCount()->Int{ return 0 }

    func getS2RowCount()->Int{ return 0 }

    func getS3RowCount()->Int{ return 0 }

    func getS4RowCount()->Int{ return 0 }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        return setCells(indexPath: indexPath)!

    }

    func setCells(indexPath: IndexPath)-> UITableViewCell? {

        let tItem = tSections![indexPath.section]

        switch tItem.sectionType! {

        case .TableSections1:return getCellS1(indexPath: indexPath)

        case .TableSections2:return getCellS2(indexPath: indexPath)

        case .TableSections3:return getCellS3(indexPath: indexPath)

        case .TableSections4:return getCellS4(indexPath: indexPath)

        }

    }

    func getCellS1(indexPath:IndexPath)->UITableViewCell?{ return nil }

    func getCellS2(indexPath:IndexPath)->UITableViewCell?{ return nil }

    func getCellS3(indexPath:IndexPath)->UITableViewCell?{ return nil }

    func getCellS4(indexPath:IndexPath)->UITableViewCell?{ return nil }

}


7. Now Create a TableSectionDemoVC for visibility tableview and override UITableView numberOfRowsInSection and  cellForRowAt methods.

import UIKit

class TableSectionDemoVC: TableCommonVC{

    override func viewDidLoad() {

        super.viewDidLoad()

        self.setTlbNibs()

        self.tlbView.reloadData()

    }

    override func getS1RowCount()->Int{ return 1 }

    override func getS2RowCount()->Int{ return 1 }

    override func getS3RowCount()->Int{ return 1 }

    override func getS4RowCount()->Int{ return 1 }

    

    override func getCellS1(indexPath:IndexPath)->UITableViewCell?{

        if let hCell = self.tlbView.dequeueReusableCell(withIdentifier: "Cell1") as? Cell1{

            hCell.selectionStyle = .none

            return hCell

        }

        return nil

    }

    override func getCellS2(indexPath:IndexPath)->UITableViewCell?{

        if let hCell = self.tlbView.dequeueReusableCell(withIdentifier: "Cell2") as? Cell2{

            hCell.selectionStyle = .none

            return hCell

        }

        return nil

    }

    override func getCellS3(indexPath:IndexPath)->UITableViewCell?{

        if let hCell = self.tlbView.dequeueReusableCell(withIdentifier: "Cell3") as? Cell3{

            hCell.selectionStyle = .none

            return hCell

        }

        return nil

    }

    override func getCellS4(indexPath:IndexPath)->UITableViewCell?{

        if let hCell = self.tlbView.dequeueReusableCell(withIdentifier: "Cell4") as? Cell4{

            hCell.selectionStyle = .none

            return hCell

        }

        return nil

    }

}

 

Overview

Table views in iOS display rows of vertically scrolling content in a single column. Each row in the table contains one piece of your app’s content.
For example, the Contacts app displays the name of each contact in a separate row, and the main page of the Settings app displays the available groups of settings.
You can configure a table to display a single long list of rows, or you can group related rows into sections to make navigating the content easier. :- https://developer.apple.com/documentation/uikit/uitableview

How to Create UITableView with multiple sections in iOS ,Binding a list with multiple sections and different cells,Build Sections With Nested Types UITableView,UITableView with multiple sections swift 5 ,Dynamic sections UITableView example swift 5,UITableView swift uitableview style grouped programmatically swift ,Set section in uitableview swift


Comments

Post a Comment