Detect section of UICollectionViewCell - ios

I have got UICollectionView with custom layout, so there is a row of items, that are scrolling with paging. Also there is a multiple sections, so the final view is like an App Store example of final view is as below,
Problem is, to place in each section footer a page control, that will detect current page of section. I don't know how to detect in witch section scroll view offset was changed. Will appreciate any help or another solutions!

You can get the section and row in which the collection view was selected using the following delegate method.
// Swift
func collectionView(_ collectionView: UICollectionView,
didSelectItemAtIndexPath indexPath: NSIndexPath)
// Obj-C
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Here, this method is invoked on the delegate only when a cell is selected. It returns indexPath.
indexPath contains two property you are asking for right now.
indexPath.row - Gives the item currently selected item index in section.
indexPath.section - Gives the current section being used in the collectionView
Note: Counting Starts from 0, not 1.
So if I selected "ComboCritters" ( Look image above), I would get 0 for indexPath.section referring to the "New Games we love! " and, 2 for indexPath.row saying the 3rd item is selected.
And, that's how you get the selected section in your app.
Kind Regards,
Suman Adhikari

You can't have the three sections highlighted in red in your diagram as separate sections in the same collection view if you want them to independently scroll. Scrolling happens to the entire collection view, not to one section of it, unless you have a very complicated layout subclass.
It would be far simpler to have three collection views, one for each section.

Related

How to implement collection view like this?

I am trying to implement collectionview which have functionality when I click on plus cell a new cell will be added and when I click on cross icon cell will be removed and plus cell needs to be moved to the removed cell position,here I am attaching design for reference
design here
You need two separate cell layouts for this design.
In the function that returns the number of items in your UICollectionView, simply add one for the last cell.
For object 0..n (where n is the number of data objects in your collection) you then return your custom data cell. For object n+1, you return the cell layout for the "New Item Cell".
You should tak a look at the UICollectionViewDataSource protocol.
collectionView(_:numberOfItemsInSection:)
UICollectionViewDataSource

Swift Button That Outputs UICollectionView Cells One by One?

I made a UICollectionView, and everything is working. It makes 100 cells that I can scroll through in simulator with no problem.
However, rather than seeing all the cells at once, I want the cells to be released one by one whenever that red button is pressed.
I am confused because I noticed in the storyboard, it hard codes the number of cells it has on the screen at once. Is there any way to get around this?
Thank you!
This is what the UI looks like in storyboard.
This is the code I used to make it. It's basic, and just says to fill the text box of the cell with a string from the array.
Your question is garbled.
A collection view has a delegate and a data source. The data source responds to messages in the UICollectionViewDataSource protocol. That protocol lets the collection view ask how many sections it has, and how many rows in each section, as well as asking for the cells from those sections and rows.
There are also methods that let you tell the table view that you want to add more cells. Take a look at the method insertItems(at:). That lets you provide an array of indexPaths, which tells the table view that you have added new entries.
You could certainly write a button action method that added one or more entries to your data model and then used the insertItems(at:) method to notify the collection view that it had new entries. If there was room in the content view of the collection view to display additional cells it would then call the data source and ask for new cells at those index paths.
Sounds like you just need to keep track of how many items you want displayed (which will increase the more that button is pressed) and use that in your UICollectionViewDataSource method. Something like:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return min(myRunningItemCount, maximumNumberOfItems) //assuming there's a maximum
}
Then you just need to call reloadData on the collection view whenever that number changes.

How to not reuse UITableViewCell and keep cells in memory after they disappear from the view?

I have a "not so big" dynamic tableView with a number of rows that can vary between 10 and 20.
I used Storyboard and created 4 different cell prototypes which all have their own identifier. Most of these cells have UITextFields in them and the user will enter information such as e-mail adresses etc...
I use dequeueReusableCellWithIdentifier: in tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) to create my cells.
However whenever I scroll my tableView I can see that the appearing cells get the data from upper cells, because cells that disappear are enqueued for reuse.
Given the fact that I have a limited number of cells, I would like to get rid of the reusing feature and just have my set of cells with their own data inside. When I scroll down I see cells that correspond to the lower cells, and when I go back up I see again my upper cells.
How can I do that?
and how can I then create a cell that corresponds to a prototype from the storyboard without using dequeueReusableCellWithIdentifier: ?
I have already searched among similar question here in StackOverflow and tried to keep an array of my cells and just return the correct element if it already exists from tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) but nothing worked so far
You are overthinking this a bit. If you think about what dequeueReusableCellWithIdentifier: does, it just grabs a cell that it finds with that specific identifier. If you want every row to have it's own unique cell that is not used by any other row, just give each row it's own identifier. The simplest way would be to just to stringify the indexPath row, or both the row+section if you have multiple sections.
Something like this should do it:
let cellIdentifier = String(indexPath.section) + "-" + String(indexPath.row)
In these cases I will typically create what would be the content of the table view cell in its own view controller or custom view, something like "SignupFormViewController", and store that as a property on the view controller that is managing your table view. In your case this could also be multiple view controllers of this type in several properties or an array. Then dequeue your cells as normal but treat them as dumb containers to which the view controllers' views are added as subviews. Then you'll just to have tie up all the loose ends—removing the view from the cell when the cell dequeued, not adding a view if the identical view is already a child of the cell, constraints to match cell size, etc.

UICollectionView: Get indexPath of Cell that is not visible

I have a UICollectionView with several cells – there are always some of them outside the viewport, so there are cells being reused.
When an event occurs I want to take a specific cell and move it to the front of the UICollectionView. In order to make moveItemAtIndexPath() work I need to know the current indexPath of the Cell that I want to move.
When I initialize the Cells in the CollectionViewDelegate with the cellForItemAtIndexPath method I save its indexPath into a variable of the Object that is the model of the Cell (not it’s actual Object, just the model). Then I look at this variable when I want to move the cell.
This is working fine as long as the cell was visible once – it seems like it only initiated then, even though it is part of the CollectionViewData all the time.
How can I make sure my CollectionViewCell has an indexPath, even when it is not visible or has not been visible yet?
If you know what data (cell) you want to present at the front (I assume top) of your UICollectionView, why don't you just update your dataSource and reload your UICollectionView?

How to distinguish between UITableView custom section click and section first row click?

UITableView returns the same callback "didSelectRowAtIndexPath" with the same NSIndexPath (0,0) both for section click and section first row click. I'm using custom view for section header view and I need to perform some action on these section rows. Tried checking cell class with [tableView cellForRowAtIndexPath:indexPath] but it's obviously returning the same row cell instead of section cell. Any suggestions?
UPDATE I could add my custom section view to first row instead of adding it as a section, however in that case, I would need to return different row height in "heightForRowAtIndexPath" and that would be not-performanc-wise decision.
UPDATE I'v designed my section view as a subclass of UITableViewCell, because I prefer to get native UITableView callbacks instead of workaround'ing with tap gestures or buttons.
Centurion, if you want to have section that you open/close easily, I suggest you to use the class APLSectionHeaderView.
You can find more information on APLSectionHeaderView.h and APLSectionHeaderView.m
Hope it will help you.
I've been using it, so if you have some question about it...

Resources