Ios swift: How to create one static cell in dynamic table view?

I would like to use tableView to have 2 static cells on top of the list of dynamic cells. As far as I understand, I should use a dynamic prototype tableView. But I do not understand how to add 2 static cells and design them, for example. Add a text box to the first and a label to the second.

What should I do in my storyboard? And what should I do inside the controller? How can I distinguish statics from dynamic cells?

Can someone give me, as a bloody beginner, some guidance on where to start? Many thanks!

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 leads to the following: enter image description here

Later, when I use real data, I will skip the first 2 rows of data ... Can I somehow "reset" the row counter after creating my static cells?

And how can I change 2 static cells? To add a text box and labels? Or should I do this programmatically?

+7
ios uitableview swift
source share
3 answers

I found help here: Mixing static and dynamic sections as grouped tables

And my solution looks like this:

one. enter image description here

  1. Add and place static cells: enter image description here

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

  3. Correct 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; } 

The final results will look like this:

enter image description here

Hope I can help someone with the same question :)

+17
source share

you can use something like this to use or display a static cell

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return numberOfDynamicCells + 1; } 

and in your cellForRowAtIndexPath data cellForRowAtIndexPath you can use something like this.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row == 0){ // go ahead to display your static Cell } else{ //go ahead to display your dynamic cells. } return yourCell; } 

here is the code for quick.

 func numberOfRowsInSection(_ section: Int) -> Int{ return numberOfDynamicCells + 1 } 

and in your cellForRowAtIndexPath data cellForRowAtIndexPath you can use something like this.

 func cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell?{ if indexPath.row = 0{ // go ahead to display your static Cell } else{ //go ahead to display your dynamic cells. } return yourCell; } 

Luck...

+4
source share

Yes, if you static cells should be IBOutlet properties, and in tableView(_:cellForRowAt:) you can return these properties for any pointer paths you want

Here is an example:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch indexPath.row { case 0: return firstStaticCell case 1: return secondStaticCell default: let cell = tableView.dequeueReusableCell(withIdentifier: "DynamicCell", for: indexPath) cell.textLabel?.text = "Dynamic \(indexPath.row + 1)" return cell } } 

enter image description here

0
source share

All Articles