Swift remove blank table view cells? - ios

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.

Related

UITableView not refreshing when numberOfRowsInSection = 0

I have a UITableView that presents a list of nearby places. I can filter the table using a UISegmentedControl by the type of place (bar, restaurant, etc.) The issue is that when I attempt to filter the list by a segment that has 0 records, the previous segments records are not being replaced with an empty table.
Every time I click on a different segment, I'm calling tableView.reloadData() and feed the table a filtered array containing places of the selected type.
From my experience , The issue lie in the logic that you have made on dynamically change of height of tableView hopefully because only cellForRowAtIndexPath will not called in case of your number of rows are 0.It reload but some logic in cellForRowAtIndex path is there which is never called.

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.

remove blank cell in UICollectionView

Right now i am working with UICollection view, which i am trying to make a grid view! for loading images
i have a array with Title, which i am displaying on toolbar by using index.section.
now my problem is. if the count of section is odd number there with be an empty space in between the sections something like this.
for avoiding this i am loading all images in one section, but now as i have one section i cannot updates my title from the title array. i am running out of idea's to handle both .
do anyone have some solution or idea?
There is more than one solution to your problem:
Create an NSMutableArray and have it contain all of your array
for different sections.
Keep using sections, but create an if
condition to check if the array count is odd. In this case, you can
stretch your last cell to fit the whole width. (I used this in one
of my apps).

Need UITableView's cellForRowAtIndexPath: to return nothing for some rows

In my code, some cells should not be shown to the user on the UITableView.
So my dataSource would supply objects, but I'd like to check in my cellForRowAtIndexPaht if the cell is hidden, and if it is, practically return nothing.
So that the cell's height would be zero, but more than that, I'd like to save my function the building of the whole cell.
I tried to return nil, but alas, it crashes.
The data source is supposed to return what is actually in the table.
You can easily make a new array to hold the data you want shown, and omit the data you do not want to see (keeping it in a master data source). Then as some rows in the table view are able to be seen, add them into the array of table data and use the insertRows method of UITableView to animate viewing the newly unveiled data.
Could you move the logic up into the numberOfSections or numberOfRowsInSection methods used to create your UITableView? Basically, whatever logic you are using to try and return null for those cells could move into the numberOfRowsInSection method in the UITableView class and return only the number of visible (not hidden) cells.
Then you would have to configure your cellForRowAtIndexPath method to hand out the cells from your data source in order by counting up through your visible rows instead of just pulling them out the array.
I wouldn't think you would want to return 'nothing' to a program expecting an object.
Link to Apple Reference Docs for UITableView, the numberOfSections and numberOfRowsInSections can easily be used to logically modify the amount of data being shown in a table view.

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