Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have one static cell in a dynamic tableview?

I would like to use a UITableView to have 2 static cells on top of a list of dynamic cells. As far as I understand, I have to use a dynamic prototype tableView. But I don't understand how to add 2 static cells and design them, eg. adding a textfield to the first and a label to the second.

What do I have to do in my storyboard? And what do I have to do inside the Controller? How can I differentiate the static from the dynamic cells?

EDIT: I tried this for testing:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as CardTableViewCell
    
    //static cell
    if (indexPath.row < 2) {
        cell.dyn.text = "static \(indexPath.row)"
        return cell;
    }

    // Configure the cell...
    cell.dyn.text = "buh"

    return cell
}

this results in this:

enter image description here

Later when I use real data I will miss the first 2 data rows... Can I somehow "reset" the row counter after I created my static cells?

And how can I modify the 2 static cells? For adding a textfield and labels? Or do I have to do this programmatically?

like image 684
user1555112 Avatar asked Mar 09 '15 12:03

user1555112


People also ask

How to create a static cell in a table view?

To do this go to Interface builder and click on the Table View. Now you need to set the content type of the table view to Static Cells.

What is a static table view used for in Salesforce?

A static table view can be useful when creating menu’s that do not change or app settings. Pretty much anything that can be listed that will have fixed values. Custom cells for static table view are done in the exact same way as custom cells for dynamic table views.

What is the difference between UIViewController and static table view?

The biggest difference when using a static table view is that you need to do it through Interface builder, and it needs to use a UITableViewController. You cannot add a normal UITableView in a UIViewController. Now that we have the basics covered we can start implementing the static table view with custom cells.

How to increase the number of rows in a table view?

Now you need to set the content type of the table view to Static Cells. After setting it to static, you can increment the number of rows by clicking on Table View Section in the view hierarchy on the left (highlighted in blue) and then on the right you will see the option to increment the number of rows that the table view has.


1 Answers

I found help here: Mixing static and dynamic sections in a grouped table view

And my solution looks like this:

1. enter image description here

  1. Add and layout the static cells: enter image description here

  2. Give each cell a unique name and add them as outlet to the TableViewCell class

  3. Adjust the code:

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (section == 2){ // my dynamic cell is index 2
            return 5 // just for testing, add here yourrealdata.count
        }
        return 1 // for static content return 1
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        var cell: CardTableViewCell!
    
        if (indexPath.section == 0) {
            cell = tableView.dequeueReusableCellWithIdentifier("static1", forIndexPath: indexPath) as CardTableViewCell
    
            cell.cardSetName?.text = self.cardSetObject["name"] as String
    
        }else if (indexPath.section == 1) {
            cell = tableView.dequeueReusableCellWithIdentifier("static2", forIndexPath: indexPath) as CardTableViewCell // just return the cell without any changes to show whats designed in storyboard 
    
        }else if (indexPath.section == 2) {
            cell = tableView.dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as CardTableViewCell
            cell.dyn.text = "row \(indexPath.row)" // return test rows as set in numberOfRowsInSection
        }
    
        return cell;
    } 
    

End results will look like this:

enter image description here

I hope I can help someone with the same question :)

like image 87
user1555112 Avatar answered Oct 14 '22 20:10

user1555112