Cyclic loading of custom cells Swift - ios

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
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!

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.

Related

iOS Swift How to Extract Strings from All Selected Rows in a TableView

I want to loop through a TableView and extract the text from all the selected rows. I suppose I "could" create and maintain a special array that is updated every time a row is selected/deselected using the didSelect/didDeselectRowAtIndexPath methods. But creating a separate array seems like an extra step. Is there no way to let the TableView itself serve as the array and then simply loop through it and get the selected rows? What would the code look like? I'm new to Swift, so this might be a silly question.
Part of the problem is that cells are supposed to be reused, and when used this way it is not possible to loop through them all. You could get around this by using a unique reuse identifier for each cell, such as the indexPath itself or some underlying unique id in your model. Then, you could indeed loop through all cells and retrieve whatever state you desired from each.
You would, however, find your application crushed under the weight of too many cells being instantiated and kept in memory. If you don't have many cells you won't be killed, but try it with a big data set and your app will enjoy a very quick death.
It is far more efficient to store one array with a bunch of id's than a large number of memory-intensive UITableViewCells.
As mentioned in comments, you should work with underlying datasource, not the table itself.
For example if your table shows rows from Array, it is way more faster to retrieve strings directly from that array than creating UITableViewCells and get strings from them.
Get indices of selected rows using UITableView's property indexPathsForSelectedRows.
Query datasource for each row.
As has been said the tableview only handles displaying, your datasource is what powers the data shown if you think about it.
Plus as said before the tableview dequeues cells as they scroll on and off the screen.
The best way to achieve what you want is to add a property to your datasource for each element that will allow you to filter out the select properties easily.
How are you storing the state for each selected cell currently? As this is the same functionally you would use to be able to generate your selected text array.

Swift remove blank table view cells?

This question is a bit different than similar questions where it's recommended to change the background color.
I currently only add a cell if the "domain" in the JSON reads as "youtube.com". Unfortunately, it will add a blank cell if otherwise denoted. How can I fix this?
In numberOfRowsInSection you need to pass the right number of cells which you would later on display. I assume you are passing redditEntry!["data"]!.count here and then later on filtering for youtube.com domain in cellForRowAtIndexPath.
What you can do is have a filtered array from redditEntry["data"] and use this array as your table data source, so that both cells and number of cells match.

Size limit for array in UITableviewController

I populate the tableview using the data from the webservice.
Now lets say. Initially I got the 20 objects in array. So I populated 20 cells in table.
Now I scroll up the tableview and reach the last cell. Then I call webservice to call more records. This time I again got 20 records. So I populate them to table view.
Now I have array count equal to 40. If this goes on. The array size will go on increasing, which might create performance issue or memory issue.
So please tell me what is the best approach to handle this situation when I am working on native code.
Thanks.
Your approach is good and right, now it creates some cell reload problem when you have image in cell which are downloaded from server. So the best approach is add you data in array as you have describe. Just check if your cell is nil then set all data to that cell, and if cell is nil then just return cell, don't do any thing for that cell. It will just refresh your all downloaded set of array's data. In you case it will reload first 10 cell, and for next 11-20 data it will skip 0-9 cell and load from 10-19 cell.

adding a control to a custom cell view

I am getting really frustrated trying to solve this problem i tried implementing it in many many ways but no solution. I have a UIStepper in a custom cell and i want to change a value on the cell. Everything works fine expect when i scroll around the tableView the values from the UIStepper changes from one cell to another. Please help here are my screen shoots.
Link to tableview implementation and link to cell implementation
You are trying to store the stepper value inside each individual cell. That's not going to work because cells are reused; the cell that you now see in row 2 may reappear in row 20 when the user scrolls.
That is why you must store the value for the stepper in the model (your data) on a row-by-row basis, so that you can set it freshly and correctly for that row every single time cellForRowAtIndexPath: is called.
This, in turn, means that as the user steps the stepper, its valueChanged is going to need to talk to the table view data source so that the model can be updated ("the stepper value for row 5 has just been changed to 3") and maintained in the model.

iOS iterate UITableView

I have a UITableView that collects data from a database. What I would like to know is if there is some way I can iterate in the UITableView collection and check the values of the cell? The reason I ask is because I would like to update each cell based on the current value that it has (change font, size, color, etc.). I've seen in another SO post regarding this topic, but since the cells are already created and their values are changed it is a bit harder for me. I was thinking of iterating through the UITableView before I call reloadData, but any other suggestions are welcome.
You should not iterate over the cells of UITableView, because some of them (in fact, most of them) may not be present until you request them. UITableView aggressively recycles its cells, so if a cell is not visible, it is very likely that you would be creating it from scratch only to put it back into recycle queue moments later.
Changing your model and calling reloadData the way your post suggests would be the right solution. iOS will ensure that it runs the update in a smallest number of CPU cycles possible, so you do not need to worry about the cells that are already created. This is also the easiest approach in terms of your coding effort.
A table view is for displaying data. The properties of your table cells should only be written to, not read from. The appropriate way of handling this situation would be to update your underlying model objects -- the objects that you use to populate the table view -- as the data changes, and then reload the affected rows.
The issue you'll encounter is that UITableView reuses table cells. Once a table cell scrolls off the screen, it's quite likely that the table view will reuse the same cell to display a different row.
This means it's fundamentally not possible to iterate over the table cells. When you need to refresh a row because its data has changed, you should call reloadRowsAtIndexPaths:withRowAnimation: (or reloadData if all rows have changed) and if the row is visible on screen, UITableView will call your data source methods and give you an opportunity to configure the cell for display.

Resources