0

I've been trying to get some response from stack on this issue for a while now with no success.

So i will try to be very precise.

I have an uitableview with two custom cell nibs. Data source for both are two separate arrays (questions array and answers array). The first custom cell contains uilabel (question cell) and another one uibutton (answer cell).

On launch of the app a specific amount of question cells load up (depending on the amount of objects in the array) and after them the 1 answer cell loads up.

Here is the pic

enter image description here

after click answer2 - again the same procedure happens producing new question cells below as well as the answer3 cell

Don't know how to achieve that.

Thank you!

David Robertson
  • 1,561
  • 4
  • 19
  • 41
  • Try [tableView updateCell ] method. Or add new data into your data source and reload the table. – Blind Ninja Aug 29 '15 at 07:23
  • @Harvant S. when i append the data into a datasource and reload the table it just increases the quantity either of the question cells or of the answer cells - they are never loaded below as new instances. – David Robertson Aug 29 '15 at 07:24
  • What do you mean by "increases the quantity" but not "new instances"? – jtbandes Aug 29 '15 at 07:26
  • @jtbandes here is the picture of how it looks like if i simply append the datasource http://imgur.com/dCFQtWh – David Robertson Aug 29 '15 at 07:28
  • I'll suggest you to make only one cell xib or class ( I would prefer class) with two identifiers. One for question and another for answers. Arrange your data according to you requirements and identify on run time that I want here an answer or a question. Get respected cell. Offcours your cell will be same but cause of different ids it will load different ui. You can manage ui according to your id in your cell class. Nothing much needs to be done. – Blind Ninja Aug 29 '15 at 07:30
  • One thing these ids will not be the 'cell identifier'. It will like a flag which help to identify what kind of cell is. – Blind Ninja Aug 29 '15 at 07:32
  • There are two methods for cell- one datasource method for load cell ( cellforrowatindexpath) and one another delegate method to customize your cell on before it display. These methods will help you. You can manage easily. ( I forgot what the other method is and I am currently on my phone) – Blind Ninja Aug 29 '15 at 07:38
  • @HarvantS. okay, thanks. – David Robertson Aug 29 '15 at 07:39
  • @HarvantS. actually i can't find the delegate method you've mentioned. and it's what keeps me from implementing your approach. – David Robertson Aug 29 '15 at 08:09
  • Here is http://stackoverflow.com/q/31988450/2071323 – Blind Ninja Aug 29 '15 at 09:21
  • Sorry I was at somewhere in halfway of my journey. – Blind Ninja Aug 29 '15 at 09:22
  • It's totally fine. Thank you so much for help – David Robertson Aug 29 '15 at 22:24

1 Answers1

1

This is not that hard as it seems. So, on the launch of the app, you load the initial questions and answers. For that you would need the number of required cells, say, 3 questions and 1 answer, totally you need to return 4 from the numberOfCellsInSection:. And, when the user taps(selects, press) the answer row, you just increase the number of cells on the table, if you have 2 questions and 1 answer on the next cycle, now you would return 7 from the numberOfCellsInSection: datasource method of the tableView. And so on.

Also, you can divide the each question-answer into the sections, if you want to have more organized way of achieving your goal. In this case, you have to increase the number of sections in the table, and return proper number of cells for each section on answer cell selection. Use numberOfSectionsInTableview: and numberOfCellsInSection methods of the datasource for this.

Don't forget to draw (set proper texts to the cell's labels, image if any) the data on cellForRowAtIndexPath: method to reflect the correct data on your cells.

Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
  • yes, but it doesn't work the way i want it to. i'm doing it absolutely similarly (be it a simple `numberofRowsInSection` approach or `numberOfSectionsInCell`). yet the result - I'm getting is http://imgur.com/dCFQtWh – David Robertson Aug 29 '15 at 08:08
  • It seems like you have implemented the mentioned methods in wrong way. You have to re-check the implementation of the `numberOfRowsInSection:` method to make sure your returning the correct number of cells for the section. Also, check your `cellForRowAtIndexPath:` method to feed the cells with correct data. – Fahri Azimov Aug 29 '15 at 08:17