0

I have a UITableView, which as an example contains dynamic cells I create based on the content of an array.

I can populate these using the count of the array and indexPath to render a cell per item. I am happy with this and it works well.

I would like to try now and create static cells programmatically.

Immediately however I am stumped, how do I create this? I'm currently overriding numberOfRowsInSection and cellForRowAt indexPath as follows:

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! ProfileCell
    cell.rowContent.text = items[indexPath.row]
    return cell
}

I suspect my first mistake is dequeueReusableCell and would really appreciate any help.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Harry Blue
  • 4,202
  • 10
  • 39
  • 78
  • 1
    Possible duplicate of [Setting static cells in uitableview programmatically](https://stackoverflow.com/questions/11192736/setting-static-cells-in-uitableview-programmatically) – Tamás Sengel Jan 16 '18 at 17:51
  • That is Objective C. This is Swift. – Harry Blue Jan 16 '18 at 17:53
  • 1
    The answer to your question is the same in both languages (it's not possible and does not really make sense). [See this answer by MikeS.](https://stackoverflow.com/a/11192919/3151675) – Tamás Sengel Jan 16 '18 at 17:54
  • It's not possible to create a UITableViewCell programmatically? What doesn't make sense? I'd like to create 5 cells, all with a different type of content. I don't want to re use a cell. It can be done with a storyboard, so.... – Harry Blue Jan 16 '18 at 17:56
  • @pgGriff The code in your question creates one type of cell. What is preventing you from expanding that code to handle 5 types of cells? – rmaddy Jan 16 '18 at 18:05
  • That is my question, I would like to do this, however do not understand how. – Harry Blue Jan 16 '18 at 18:05
  • 1
    "What doesn't make sense?" ... The term "static cell" has a very specific meaning, namely one that always exists when the table view is instantiated, either with fixed design or with `@IBOutlet` references. But if you want to add one programmatically, you're just creating a cell dynamically. Whether you want to create that cell from a cell prototype or build it programmatically yourself, is up to you. But it isn't a "static cell." – Rob Jan 16 '18 at 18:09

1 Answers1

2

If I understood your question, you want to add a static cell in a tableView that contains dynamic cells.

If that is the case, you could hardcode this, increasing the return value here:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return items.count+1 
}

In this case, you want to add just one static cell.

In cellForRowAtIndexPath, you should define where you want to add this static cell. In the example below, it would be the first cell:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   if indexPath.row == 0 {

     //should change StaticCell to the static cell class you want to use.
     let cell = tableView.dequeueReusableCell(withIdentifier: "staticCell", for: indexPath) as! StaticCell

     //hardcore attributes here, like cell.rowContent.text = "A text"
     return cell

   }

   let row = indexPath.row-1

   let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! ProfileCell
   cell.rowContent.text = items[row]
   return cell
}

It's basically about shifting the items[row] according to the number of static cells you want to use. I'm not sure if that will work, but would be my first guess (according to my experience about that). Try this and tell me if it worked :)

VSMelo
  • 355
  • 1
  • 5