0

I need to have a view which has a UITableView and the main UITableView has a cell that has another tableview. I have added the view on storyboard and implemented the delegate and datasource methods in a single viewcontroller. But the tableview methods are not getting called for the inner tableview since i have set inner tableview delegate and datasource like this:

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

    switch tableView.tag {
    case 1:
        if indexPath.section == 0 {
            let pendingReportsCell = tableView.dequeueReusableCellWithIdentifier("PendingReportsCell") as! PendingReportsTableViewCell
            pendingReportsCell.taxYearTableView.delegate = self
            pendingReportsCell.taxYearTableView.dataSource = self
            taxTableView = pendingReportsCell.taxYearTableView
            taxTableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: "EarliestMonthTaxYearCell")
            tableView.tag = (taxTableView?.tag)!
            taxTableView?.reloadData()
 }

Please tell me an alternate to do this.

Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49
Roshni
  • 153
  • 2
  • 14
  • Why you need table view within table view? – Tushar Sharma Feb 11 '17 at 10:38
  • you want to create menu ? with sub menu option – Himanshu Moradiya Feb 11 '17 at 10:41
  • The UI Design is such that I need another tableview within another. No subviews are there. Just a tableview with 2 sections. The first section again having two sections and data within it. – Roshni Feb 11 '17 at 10:43
  • @Roshni can you be more specific what is the requirement.What is that you are trying to design? – Tushar Sharma Feb 11 '17 at 10:50
  • I cant share the design here. but a brief idea about what I want to do. I have a tableview with two sections: Pending and Completed Reports. The Pending Reports again has two sections for Current and Previous years with years inside cells. – Roshni Feb 11 '17 at 10:55
  • Possible duplicate of [Is it possible to add UITableView within a UITableViewCell](http://stackoverflow.com/questions/17398058/is-it-possible-to-add-uitableview-within-a-uitableviewcell) – Himanshu Moradiya Feb 11 '17 at 11:01
  • http://stackoverflow.com/questions/31065920/adding-uitableview-inside-uitableviewcell look this @Roshni – Himanshu Moradiya Feb 11 '17 at 11:04
  • ok will check it. Thanks @Himanshu – Roshni Feb 11 '17 at 12:01

1 Answers1

0

Set the delegate and datasource in your custom cell class as below:-

       class PendingReportsCell: UITableViewCell {
         // set the delegate & datasource of taxYearTableView in the init or  
         // awakefromnib or storyboard. 
         // I have set it in storyboard and it works for me. 
         // Implement the datasource and delegate methods of taxYearTableView in this class.
         }

When taxTableView?.reloadData() is called it will invoke the above delegate and datasource methods

iAviator
  • 1,310
  • 13
  • 31