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.
Related
I have table view with multiple different cells, and one of the cells depends on property of another one. I.e. I have FirstCell, which has dynamic property count, and I have SecondCell, which contains several UITextFields. I want the number of these UITextFields to match the count property. How can I get count property from FirstCell considering that this property can be changed?
I.g. FirstCell contains count = 3, SecondCell shows three UITextFields. Property count changes to value 4 and another one UITextField appears in SecondCell.
The value of count should be in your data model. You add an observer, or put code into the setter, so when the value of count is changed, the two cells depending on it (the one displaying count, and the one showing the text fields) are both reloaded; that's reloadCellAtIndexPaths or something like that from memory.
Obviously the code that loads cells must be written correctly, and code changing count in the first cell must change the model property.
Thinks like this should be managed by the UI(Table)ViewController. The view controller should be notified when the text field changes (for example using target-action) and then update the data source and reload the relevant 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.
I have a custom view that exists in a cell in a tableview. The view is called bulletRow and it is a series of bullets that can be filled in or emptied when a user taps on them. Each cell in my tableview contains some bulletRows and I need to save them when the user taps on them. I have considered using Core Data, but I don't need them to persist when the app is shut down, I only need it to exist when the user scrolls past the dequeueing point.
Here is my situation right now: The default state for bulletRows is to have 5 dots, all of them empty. When a user taps on them they become filled. If the user scrolls down however, they get reset back to being empty. How can I save the state of the bulletRows?
The bulletRows have a property called numberOfFilledCircles which can be set at anytime to change the amount of filled in circles. This is all done in Swift as well.
In general, you should use something, such as an array, to hold the state of your table. The cells in your table should reflect that state, and update that state when selected.
You might start with an array of integers in your table view controller, like this:
var numberOfFilledCircles = [Int]()
Use the number of items in your array to determine how many rows to display in your table, by returning numberOfFilledCircles.count from your numberOfRowsInSection method.
You can populate the array in viewDidLoad. If you're hardcoding the rows, you can repeat this statement for as many rows you want:
numberOfFilledCircles.append(0)
Each Int in the array holds the value representing how many circles are filled (initialize to 0).
In your cellForRowAtIndexPath, use the appropriate value from your array when constructing your cell. For example, if your cell had a UILabel called numberOfFilledCircles, you would do this:
cell.numberOfFilledCircles.text = String(numberOfFilledCircles[indexPath.row])
Finally, in your didSelectRowAtIndexPath, update the array with the number of circles you want filled in:
numberOfFilledCircles[indexPath.row] = //whatever you want
The issue here is that the UI is not the model.
When the ui elements are pressed, you should send an action to the underlying model to update its state, and when cells are dequeued you should restore the checkbox state from the appropriate model element.
I have an indexed table view in which the cells are grouped alphabetically into sections. I would like to be able to only load a block of the data associated with a particular section when that indexed section is selected. In other words, my table view is the type where there is a selectable field at the right side of the table view that contains the letters A-Z. You can select the letter P for example to jump to cells that have content that starts with the letter P.
Is there any way to detect the selection of an indexed section so that I can then reload the cells in that section once I load the block of data associated with that section?
As the user uses the index down the side, the UITableViewDataSource tableView:sectionForSectionIndexTitle:atIndex: method will be called.
You can add logic to this method that if this is the first time you've seen a given section index, that you need to load the data for the section.
But keep in mind that long before this you would have already told the table how many total sections there are and how many rows are in each of those sections. So long before your table even shows the index down the side, you need to have at least loaded counts for all of the sections but not necessarily the detailed data.
Also keep in mind that a user can slide their finger down the index list. This means the table will want to jump to each and every section as the user slides their finger. So whatever lazy loading you do needs to deal with this in a nice manner (not making the UI sluggish).
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.