Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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. 



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() {



This post first appeared on IOS Developer Live, please read the originial post: here

Share the post

How to Use Multiple Sections in UITableView iOS Swift !

×

Subscribe to Ios Developer Live

Get updates delivered right to your inbox!

Thank you for your subscription

×