Load Data from Scrolling to bottom last cell in UICollectionView Calling API with Activity Indicator (Swift4) - swift4

I am using UICollectionView in my project. I am calling API to load data in that CollectionView. and I want to load more data when scrolling down to the bottom of UICollectionView with bottom Activity Indicator. My API is in Pagination. Simple Example it is like Soficalmedia App Scrolling. Facebook, Instagram. Scroll down to load Previous data. Can you anyone help me to do this in an efficient way.
I tried a code but it is not working properly: from URl: Load More data on Scroll Demand on CollectionView
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if indexPath.row == numberofitem.count - 1 { //numberofitem count
updateNextSet()
}}
func updateNextSet(){
print("On Completetion")
//requests another set of data (20 more items) from the server.
DispatchQueue.main.async(execute: collectionView.reloadData)
}

Related

Swift reconfigure collectionview cells

so I am trying to change the background color of the collection view cell that is clicked, however, when I call the CollectionView.reloadData() method the backgrounds don't change. I am thinking that reloading the data only checks for adding and deleting cells, but does not actually call the configure function for each cell again. I was wondering how I can reconfigure these cells so that the background color can change for the selected cell. Thanks for the help in advance!
This is the method that I call:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
...
bagCollectionView.reloadData()
}
Instead of calling reloadData, you can try:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = yourColor
}
Even though you do your logic in did select by finding a cell or implement logic in didSelectItemAt and didDeSelectItemAt, I suggest you should store the Index. Due to reusability of cell, there is chance that selected cell do not show color on scroll or if you have large number or records.
So save index in array(for multiple selections) or a single variable(single selection) and manage logic for the same.
For example:
cell.backgroundColor = selectedIndex == indexPath.item ? .red : .yellow

How to customize the 1st and 3rd cell in a custom collection view

I have a ui collection view returning custom cells of posted images - I want to round the corners of only the 1st and 3rd cell in the top row returned and nothing else. I checked the indexPath but is the same for multiple cells so it rounds multiple rows of cells.
I would add the customization inside willDisplayCell :
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if indexPath.row == 1 || indexPath.row == 3 {
cell.roundCorners() // or whatever code you use for this.
}
}

Slow video loading inside collection view cells

I have a collection view that plays short videos (3-5 sec) on each cell with full screen. The first cells take around 4 seconds to load, but the next ones load faster. The user experience is very poor because the app seems frozen at the beginning.
I tried different things but the result is the same so far. I tried to play the videos inside each cell instance and also from cellForItemAt an willDisplay. For the video player I am using a library called SwiftVideoBackground. I also tried to play videos with AVPlayer and also what this thread says.
Each cell has a button to play another video, which also takes around 3 seconds to load.
I am streaming video files from Amazon AWS S3 using AWSCloudFront
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let cell = cell as? UserFeedCollectionViewCell {
cell.videoBackground.play(view: cell.videoPlayer, url: cell.contentUrl, isMuted: false)
cell.videoBackground.restart()
}
And inside UserFeedCollectionViewCell:
#IBAction func playVideoBtnPressed(_ sender: Any) {
videoBackground.pause()
videoBackground.play(view: videoPlayer, url: contentAUrl)
videoBackground.restart()
}

How to check if the collection view cell is clicked

I want to check if the collectionview cell is clicked
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
//code..
}
Well you already have the key to your problem, it's just you don't know how to use it.
So every UIKit element has it's own delegate which is called when some event occurs.
In case of collection view, whenever cells are clicked didSelectItemAt will be called.
Here is an example
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
print("Cell \(indexPath.row + 1) clicked")
}
Considering above code just imagine you have 5 cells,
Whenever you click on any cell the above code will execute.
If you click on Third cell it will print: Cell 3 clicked
And if you don't click any cell nothing will happen.
Hope now everything is clear, Happy coding :)

NSCollectionView only renders first 100 cells

I'm trying to display 256 cells in a NSCollectionView. I'm using Storyboard but the NSCollectionViewItem is in a separate xib. It is registered in the CollectionView at startup.
A DataSource object holds an array (NSMutableArray) with numbers which are loaded into the representedObject of a NSCollectionViewItem when one is provided by the makeItemWithIdentifier(...) function.
func collectionView(collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArray.count
}
func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem {
let item = collectionView.makeItemWithIdentifier("CellItem", forIndexPath: indexPath) as! CellCollectionViewItem
item.representedObject = dataArray[indexPath.indexAtPosition(1)]
item.index = indexPath.indexAtPosition(1)
item.dataSource = self
return item
}
When the cells should be displayed only (exactly) 100 cells are rendered instead of all 256. At no point does the data array hold 100 items. Furthermore, my debug output shows that actually all NSCollectionViewItems are produced. That means the problem must lie in the rendering of the collection view. Is there a restriction to the amount of items the NSCollectionView can render at once? What am I missing?
The remaining cells appear when the window is slowly resized by the user and disappear again if the user resizes the window fast. Does the view stop rendering after a fixed amount of time?
Cells appearing on window resize and disappearing again after a click on a cell (which triggers a reload of the reloadData() function