How to deal with dynamic Sections and Rows in iOS UITableView
<p><strong>My issue</strong></p>

<p>When I deal with <code>UITableView</code>, I basically have an Array <code>table_data</code> with my sections and rows. </p>

<pre class="lang-json prettyprint-override"><code>[
   {
      title : "section A title",
      rows: [
          {data: row_1_data},
          {data: row_2_data}
      ]
   },
   {
      title : "section B title",
      rows: [
          {data: row_1_data},
          {data: row_2_data}
      ]
   },
]
</code></pre>

<p>I use <code>heightForHeaderInSection</code>, <code>cellForRowAtindexPath</code>, <code>titleForHeaderInSection</code>, <code>didSelectRowAtindexPath</code> methods like this</p>

<pre class="lang-swift prettyprint-override"><code> if indexPath.section == 0 {
        //section A
        if indexPath.row == 0 {
             //do something with table_data[0]["rows"][0]["data"]
        }
        elesif indexPath.row == 1 {
             //do something else
        }
 }
 if indexPath.section == 1 {
      //do something for section B
 }
</code></pre>

<p>Working with numbers <code>0</code>, <code>1</code>, etc becomes a headache, when my <code>table_data</code> array is dynamic. Dynamic means, that some sections or rows can have different positions or don't exist at all. </p>

<p>For example, I remove A section and my array is </p>

<pre class="lang-json prettyprint-override"><code>[
   {
      title : "section B title",
      rows: [
          {data: row_1_data},
          {data: row_2_data}
      ]
   }
]
</code></pre>

<p>I do like this</p>

<pre class="lang-swift prettyprint-override"><code>self.section_A_position = 0
self.section_B_position = 1
func afterRemoveSectionA() {
    section_A_position = -999
    section_B_position = section_B_position - 1
    //write here another new sections for -1 
}
</code></pre>

<p>Add another section C as 0 element </p>

<pre class="lang-swift prettyprint-override"><code>self.section_C_position = -999
func afterAddSectionC() {
   section_C_position = 0
   section_A_position = section_A_position + 1
   section_B_position = section_B_position + 1
}
</code></pre>

<p>And then use <code>section_positions</code> in functions</p>

<pre class="lang-swift prettyprint-override"><code> if indexPath.section == section_A_position {
        //section A
        if indexPath.row == 0 {
             //do something with table_data[0]["rows"][0]["data"]
        }
        elesif indexPath.row == 1 {
             //do something else
        }
 }
 if indexPath.section == section_B_position {
      //do something for section B
 }
</code></pre>

<p>It looks pretty simple, but when I have many sections and many cases to hide or move it in array, it becomes difficult to control and add new types of sections. Sometimes I create <code>section_positions_map</code> array to store it together and make <code>+1</code>, <code>-1</code> operation in loop. But it doesn't help it is still difficult to organize it in every TableViewController, when I need this behavior.</p>

<p><strong>Question</strong></p>

<p>Do you know any approaches or frameworks that make this part easier? </p>

<p><strong>My Ideas</strong></p>

<ol>
<li>Add <code>type</code> property to my Dictionaries </li>
</ol>

<pre class="lang-json prettyprint-override"><code>{
  title : "section A title",
  rows: [
      {data: row_1_data},
      {data: row_2_data}
  ]
  type : "section_a"
}
</code></pre>

<p>and check <code>if table_data[0]["type"] == "section_a"</code> (or use <code>enum</code> to collect types)</p>

<ol start="2">
<li>Subclass my Dictionary and check</li>
</ol>

<p><code>if table_data[0] is SubClassSectionA</code></p>

<p>but both of them looks ugly for me.</p>