0

In my scenario, I have a static tableview. Now I need to add dynamic tableview at the bottom of static tableview. As per my scenario, I can’t able to mix both in one tableview. So, I have to add dynamic tableview bottom of static tableview footer or any other bottom accessory view. I tried below methods

  1. Added footer view in static cell tableview and within that footer I have added dynamic tableview but the dynamic tableview not expanding the footer height based on its cell count also, it is not allowing dynamic cell selection.
  2. Added accessory view but don’t know how to add it at bottom of the tableview

My StaticTableview Cell Code

override func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return " "
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (section == 0) {
            return 2
        } else if(section == 1) {
            return 1
        } else if(section == 2) {
            return 3
        } else {
            return 1
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as UITableViewCell
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.section == 0 && indexPath.row == 0 { } 
        else if indexPath.section == 0 && indexPath.row == 1 {
        } else if indexPath.section == 1 && indexPath.row == 0 {
        } else if indexPath.section == 2 && indexPath.row == 0 {
        } else if indexPath.section == 2 && indexPath.row == 1 {
        } else if indexPath.section == 2 && indexPath.row == 2 {
        } else if indexPath.section == 2 && indexPath.row == 3 {
        } else {
        }
    }
iosdev
  • 125
  • 2
  • 17
  • What's stopping you from having two table view controls? – El Tomato Dec 27 '19 at 05:50
  • @ElTomato Scrolling only the problem if I maintain two tableview. Now I am adding another one static cell into static tableview within that cell I am trying to add dynamic tableview. – iosdev Dec 27 '19 at 05:53
  • Create 2 UITableView so you can achieve your goal. without this you can't do it. – AyAz Dec 27 '19 at 06:14
  • @AyazAkbar how about tableview within static tableview cell? https://stackoverflow.com/questions/17398058/is-it-possible-to-add-uitableview-within-a-uitableviewcell – iosdev Dec 27 '19 at 06:15
  • @iosdev so you need to create UItableView within UITableViewCell. this is proper way. – AyAz Dec 27 '19 at 06:17
  • @AyazAkbar Can I create it right? It is a proper way or anything problem have a chance to face? – iosdev Dec 27 '19 at 06:19
  • @iosdev yes you can, it will't not be any problem. – AyAz Dec 27 '19 at 06:24
  • add UI tableview inside of your last cell of static tableview cell.. – Shivam Parmar Dec 27 '19 at 06:32
  • @ShivamParmar I have added but can't able to click the cell. Could you please provide some sample? – iosdev Dec 27 '19 at 06:39
  • Do you need to add dynamic table view as the final cell of the static tableview or just add it after the static tableview? – Tharindu Ketipearachchi Dec 27 '19 at 06:43
  • @TharinduKetipe I have static tableview cell. Here into the static tableview last cell I need to add dynamic tbalview with user interaction available – iosdev Dec 27 '19 at 06:45
  • For the final cell of the static tableview add a dynamic tableview and then set delegate and data source. It should work for sure. – Tharindu Ketipearachchi Dec 27 '19 at 06:50

2 Answers2

0

Create a single view project add tableview inside storyboard and set up its datasource and delegate

do code as below to firstViewController.swift

import UIKit

  class firstViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

  override func viewDidLoad() {
      super.viewDidLoad()
            }

  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }

  func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      return 3;
  }

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return 1;
  }

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      var cell:CustomCell? = tableView.dequeueReusableCellWithIdentifier("Cell") as?  CustomCell
      if cell == nil {
         cell = CustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
      }
      cell?.dataArr = ["subMenu->1","subMenu->2","subMenu->3","subMenu->4","subMenu->5"]
      return cell! 
   }

   func  tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
       return 150.0
   }
}

create a new file CustomCell.swift which is the subclass of UITableViewCell and do not select with xib this file is without .xib file table and its cell will be created programatically.

do code as below to CustomCell.swift

import UIKit

  class CustomCell: UITableViewCell,UITableViewDataSource,UITableViewDelegate {

  var dataArr:[String] = []
  var subMenuTable:UITableView?
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
      super.init(style: style , reuseIdentifier: reuseIdentifier)
      setUpTable()
  }

  required init(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
      setUpTable()
  }

  override func awakeFromNib() {
      super.awakeFromNib()
      // Initialization code
      setUpTable()
  }

  func setUpTable(){
      subMenuTable = UITableView(frame: CGRectZero, style:UITableViewStyle.Plain)
      subMenuTable?.delegate = self
      subMenuTable?.dataSource = self
      self.addSubview(subMenuTable!)
  }

  override func layoutSubviews() {
      super.layoutSubviews()
      subMenuTable?.frame = CGRectMake(0.2, 0.3, self.bounds.size.width-5, self.bounds.size.height-5)
  }

  override func setSelected(selected: Bool, animated: Bool) {
      super.setSelected(selected, animated: animated)
      // Configure the view for the selected state
  }

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return dataArr.count
  }

  func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      return 1
  }

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cellID")

    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellID")
    }

    cell?.textLabel?.text = dataArr[indexPath.row]

    return cell!
  }
}

I hope it will work for you ...:)

Shivam Parmar
  • 1,520
  • 11
  • 27
0

Just add sections and rows in the current tableView and use different cell for those rows if needed and reload. don't make 2 tableView one below another.

var sections = 4

override func numberOfSections(in tableView: UITableView) -> Int {
        return sections
    }

// sections = 5
// tableView.reloadData()

and change sections on run time and handle the data source and delegate method accordingly and reload.

Shabbir
  • 289
  • 2
  • 6