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.
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 “getCellNibs” method. 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() {