UICollectionView Will Not append Cells - ios

When I add objects to my datasource, I am not able to see the added objects in my collectionview.
I am able to see in the numberOfItemsForSection increment in value, but the corresponding cells do not display the objects. The indexpath.row does not exceed its original value that I set before.
HALP.

Related

How to pass data between two cells (swift)?

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.

Swift: Get value of multiple selected UICollectionViewCells

I have a UICollectionView with multipleSelection allowed. How would I retrieve the value of the labels stored in each selected cell?
So if I had three cell: "first", "second", "third"
And I selected the first two, it would return an array equal to: ["first", "second"].
Access the selected items using indexPathsForSelectedItems. In your example above, that should return you an array with the first two index paths for row 0 and row 1 (assuming "first", "second", and "third" are actually in that order). Iterate through that array and use each indexPath contained within to grab the cells using cellForItemAtIndexPath:. Now that you've got the cells, you can grab the labels by casting the resulting cells as YourCustomCell and accessing the label property.
EDIT: #rdelmar makes a good point in the comments below. Your first resort should always be to avoid using the cells as a source of info and rather reference the dataSource providing info to the cell in the first place. However, should you need to obtain a reference to the cell for some purpose NOT addressable by examining the dataSource (calling one of the cell's methods, etc), the steps above are the way to go.

In Swift, how should I save data from a custom view so that it is not deleted when a cell is dequeued?

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.

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.

How to properly sort rows in UITableView by value of a cell's subview (custom added label)?

How to properly sort rows in UITableView by value of a cell's subview (custom added label)? I thought I would find the answer on stackoverflow, but I can't. Am I missing something?
I have a tableView which uses an xml (plist) file as a data source. Every tableView cell also has an added label which shows the distance from current to some other location and I just want to sort the rows by using that distance (this value isn't saved in plist file, it is calculated in realtime). I just want to know the theory behind this. Do I need to write the distance in plist file, put the data from plist in array and sort that array? Can I sort the rows somehow directly in tableView, in realtime?
I didn't find any alternative way, I had to save the distance value inside the plist/array and then sort the array.

Resources