Search code examples
iosswifttableviewuitableview

Implement a dynamic table within a static table view (cell)


So basically I have implemented a static table so far and at the very bottom, I want to add another dynamic table for the participant list of the event. Check out the screenshot here:

Screenshot of my Xcode project

My problem now is, that I'm not able to setup the dynamic table with its own class which is placed in the last static table view cell. You can check out the red marked squares in the screenshot. My actual plan was to give the dynamic table its own class and then retrieve the participant list as an array and setup the numberOfRowsInSection according to the count of the array etc.

Do you guys have an idea how I can implement that within a static table? Basically, how can I add that dynamic table at the bottom, including later then endless scrolling?

I've tried this post so far but it didn't completely help me: Dynamic UITableView inside static cell I'll add my solution below.

Your help would appreciated a lot!

Kind regards Matt


Solution

  • Okay thank you rMickeyD for your tip. I used it partially and implemented it. You can see the result on this screenshot here:

    Storyboard

    Adding to this I set the height of the cell in the static table with a prepareToSegue to the array of the guests.count * 44 for the cell height in the right.

    Code for the static table view (left):

    import UIKit
    class EventDetailTableViewController: UITableViewController {
    
        var attendeesArrayFromDatabase = ["Kathi", "Cansin", "Tyrone", "Manuel", "Stavros", "Christoph", "Maria", "Aditya", "Kathi", "Cansin", "Tyrone", "Manuel", "Stavros", "Christoph", "Maria", "Aditya"]
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    
            if indexPath.section == 1 && indexPath.row == 0 {
                let height:CGFloat = CGFloat(attendeesArrayFromDatabase.count * 44)
                return height
            }
            return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
        }
    
        // MARK: Navigation
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            let destViewController : EventPeopleJoinedContainerTableViewController = segue.destinationViewController as! EventPeopleJoinedContainerTableViewController
    
            destViewController.attendeesArray = attendeesArrayFromDatabase
        }
    }
    

    and the right tableview for the dynamic table:

    import UIKit
    class EventPeopleJoinedContainerTableViewController: UITableViewController {
    
        var attendeesArray = []
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        // MARK: - Table view data source
        override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            return 1
        }
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return attendeesArray.count
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("personJoined", forIndexPath: indexPath) as UITableViewCell
    
            //Create a label with the name of a person from array attendeesArray
            let attendeeNameLabel = UILabel(frame: CGRect(x: 70, y: 0, width: cell.frame.width, height: cell.frame.height))
            attendeeNameLabel.font = cell.textLabel?.font.fontWithSize(14.0)
            attendeeNameLabel.textColor = UIColor.darkGrayColor()
            attendeeNameLabel.text = attendeesArray[indexPath.row] as? String
    
            //Create a image view for the profile picture of the guest
            let attendeeImageView = UIImageView(frame: CGRect(x: 25, y: 7, width: 30, height: 30))
            attendeeImageView.layer.cornerRadius = attendeeImageView.frame.size.width/2
            attendeeImageView.layer.borderColor = UIColor.whiteColor().CGColor
            attendeeImageView.layer.borderWidth = 0.5
            attendeeImageView.clipsToBounds = true
            attendeeImageView.image = UIImage(named: "profilPicDummy")
    
            cell.contentView.addSubview(attendeeNameLabel)
            cell.contentView.addSubview(attendeeImageView)
    
            return cell
        }
    }