how to custom TableView with Header in swift

struct cellNames
{
 var isHidden:Bool
 var arrayName:[String]()
}

import UIKit
class CustomTableViewDemo : UIViewController, UITableViewDataSource , UITableViewDelegate
{

 // MARK:- ---------- OUTLET Declaration ------------
    @IBOutlet var myTableViewObj: UITableView!
   
   
    // MARK:- ---------- Variable Declaration ------------
    var myArray = [Array]()
    var cell1Array = [String]()
    var cell2Array = [Sreing]()
    var cell3Array = [String]()
    var cell4Array = [Sreing]()
   
   // MARK:- ---------- VIEW LifeCycel ------------
    override func viewDidLoad() {
        super.viewDidLoad()
       
   cell1Array = ["Data 1:1","Data 1:2","Data 1:3","Data 1:4", "Data 1:5", "Data 1:6", "Data 1:7"]
      cell2Array = ["Data 2:1","Data 2:2"]
       cell3Array = ["Data 3:1","Data 3:2","Data 3:3","Data 3:4"]
       cell4Array = ["Data 4:1","Data 4:2","Data 4:3"]
        myArray = [
  cellNames(isHidden: true, arrayName: cell1Array),    cellNames(isHidden: true, arrayName: cell2Array),    cellNames(isHidden: true, arrayName: cell3Array),
  cellNames(isHidden: true, arrayName: cell4Array)]
       
  }
 //MARK:- ------- Objective Functions ---------
 @Objc func close_action(_ sender:UIBUtton)
 {
    let section = sender.tag
    var mainIndexPath = [IndexPath]()
    for row in myArray[section].arrayName.indices
    {
  let indexPath = IndexPath(row: row, section: section)
  mainIndexPath.append(indexPath)
    }
    let isHidden = myArray[section].isHidden
    myArray[section].isHidden = !isHidden
    sender.setTitle(isHidden ? "open" : "Close", for:.normal)
    if isHidden
    {
  tableView.deleteRows(at: mainIndexPath, with: .fade)
    }
    else{
  tableView.insertRows(at: mainIndexPath, with: .fade)
    }
 } 
 
    func tableView(_ tableView : UITableView, viewForHeaderInSection section: Int) -> UIView?
    {
 let myBtn = UIButton(type: .system)
 myBtn.setTitle("Close",for: .normal)
    myBtn.backgroundColor = UIcolor.blue
 myBtn.tag = section
 myBtn.addTarget(self, action: #selector(close_action(_:)), for: .touchUpInside)
 return myBtn

    }
    func tableView(_ tableView : UITableView, heightForHeaderInSection section: Int) -> CGFloat
    {
 return 34
    }
    func numberOfSections(in tableView: UITableView) -> Int
    {
        return myArray.count
    }
   
 func collectionView(_ tableView: UITableView, numberOfItemsInSection section: Int) -> Int
    {
 if !myArray[section].isHidden
 {
   retrun 0
 }
 return myArray[section].arrayName.count
    }
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
 {
        let cell = homeScreenTableObj.dequeueReusableCell (withIdentifier: "myCell") as! myTableViewCell
       
    cell.textLabel?.text = myArray[section]. arrayName[indexPath.row]
               
        return cell
    }
}
// MARK:- my TableView cell class
class myTableViewCell: UITableViewCell
{

}

Comments

Popular posts from this blog

Windows Keys

how to send invite and give a access of selected application on App Store

how to display popover in TableView cell in swift