Hi I have been trying to add an Activity Indicator, or a Loading spinner, to the bottom of my collection view. I have searched online on how to do so but there is not a clear way to do so.
I do not want to use a cell for the indicator itself because my cells a pretty big. (2 cells take up the whole screen.) So I would just like to have a small loading spinner at the bottom for when users scroll down.
Through the research that I have done I have seen that some people use this function below and a separate class to build the spinner.
private func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) -> UICollectionReusableView{}
But I do not know how to use it. I already created a class called LoadingViewController() but in all honesty don't know if it is event he right type. Currenlty I have it set as a UICollectionReusableView
For a little more background, I already added the ability paginate data. So when I scroll down the collection view, new cells are loaded. Now the problem is I want to show a little spinner at the bottom when the data is loading because if not the app stutters and looks like it is broken.
Or is there a better way to show load items in a collection view that does not cause the scroll to freeze?
Please let me know if you have any questions regarding my situation I'll try to answer them to the best of my ability.
Related
I have a weird question regarding NSCollectionView. Basically, I have a collection view that scrolls horizontally in a vertically scrolling table view. I have implemented the data source and delegate of the collection view within my NSTableCellView subclass. The data source works just fine and the collection view is able to load some images.
Here comes the problem. I want the collection view to be selectable. I have implemented this delegate method into my table cell view subclass:
func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
// Do things here
}
This method works, but with a caveat. It only gets called when I click on an empty space in the collection view first and then selecting a cell. If I only click on a cell, it will not get called, which is quite bizarre to me. Any idea on how to fix this?
I designed the table cell view in the storyboard, the collection view has it's Selectable and Allow Empty Selection checked in the storyboard. Allow Multiple Selection is not checked because I don't want multiple selection, but I tried having it on and it doesn't change anything.
Thanks
Subclass NSTableView. In your subclass, override validateProposedFirstResponder(_:for:) to return true for your collection view and its subviews (or maybe just return true always).
I want to implement a pull to refresh feature on my current UIViewController. I have 10 buttons - 5 rows of vertical stack views in one horizontal stack view to create a grid. I would like to be able to pull to refresh here to run a function and update the button labels. What are my options?
You can't add a refresh control into a stack view for the purpose of refreshing the content.
The important thing here is you have to add the refresh control into a scroll view to let it be functional as expected:
UIRefreshControl
A standard control that can initiate the refreshing of a scroll view’s
contents.
UIRefreshControl
For this case I would encourage to let your grid to be as a UICollectionView instead of stack view.
In addition, What if you tried to add another button into the grid? the container view should be scrollable, which means that a collection view would more maintainable.
Furthermore, you might be able to get the leverage of the UICollectionViewController. Checking the following could be useful:
Is completely static UICollectionView possible?
How to create a static UICollectionView.
How to use pull to refresh in Swift?
Your best option is to go with a UICollectionView. Create a custom cell to you desired needs. After that you can add a refresh controller:
// Global variable
let pullToRefreshData = UIRefreshControl()
// Where you initialise the collectionView (eg. viewDidAppear())
collectionView.refreshControl = pullToRefreshData
pullToRefreshData.addTarget(self, action: #selector(refreshData), for: .valueChanged)
// This will be called when you pull from top of the collectionView
#objc func refreshData() {
// Maybe you need to call a server API, stop the reloading animation when you have the data
pullToRefreshData.endRefreshing()
// Refresh the data from the collectionView
collectionView.reloadData()
}
If you only need to use your implementation, maybe you can provide a context.
For this use case I'd suggest using UICollectionView, which has build in support for UIRefreshControl. But if your buttons are somewhat different or you want a bit easier and less robust solution, you could simply embed your UIStackView in UIScrollView and set it's refreshControl.
See: https://developer.apple.com/documentation/uikit/uiscrollview/2127691-refreshcontrol
Hi guys, please give me an idea how to create a list similar to the homepage of iflix (image attached below). I tried using table view with collection view inside the cell, but I don't like the performance because it flickers when I scroll because of the reloading of collection view cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CustomCell = self.tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
cell.bind(data: self.datas[indexPath.row])
cell.collectionView.reloadData()
cell.delegate = self
return cell
}
When I removed the cell.collectionView.reloadData() the scrolling is smooth but the displaying of data is incorrect because cells are reusing.
Can anyone give me a better idea on how to implement a layout like this? Thank you in advance.
there could be a variety of reasons why you are experiencing flickering, in fact the first release of that ViewController had the same issue on older devices. I would recommend first Analysing your app using the Time profiler to investigate the cause of the flickering you are experiencing. It’s very likely, as we discovered there there is not one main cause but a collection of smaller issues that when added together on the main thread causes the flickering. Below I will detail a brief description of how we manage to reduce the flickering effect.
The ViewController contains one vertical collection view, each row is its own cell that owns a horizontal collection view.
One thing we try to ensure is that drawing a cell is as efficient as possible. All cells and views are configured via ViewModels which for our case is a PODO (property only data object).
The ViewModels can be easily preprocessed on a background thread and contain e.g url, titles, hide/show buttons, cell sizes etc for any orientation. Plus it makes it very easy to test. We ensure that all images are the correct size so there is no time required to scale the image, along with that each visual item is opaque and rasterised to the correct screen scale. We also found for our code that if each cell uses ‘preferredLayoutAttributesFitting’ that can help to reduce the flicker also.
The next part is to try to help reduce the amount of effort taken to when calling reloadData. If each cell in the row is the same size then using UICollectionViewDataSource can be more process intensive than setting the item size, instead you can set the itemSize in the xib/storybaord or use a UICollectionViewFlowLayout. Before a cell is shown to the user the methods “collectionView cellForItemAt” and “collectionView willDisplay” will be called, you can try and split cell configuration work over those two method calls.
I hope that helps.
so I am exploring the new tvOS. As I wanted to kind of replicate the horizontal feature section of the app store (e.g. "Best New Apps")
My plan was to use a UITableView to lay out the base structure (rows) and use an UICollectionView for all the individual horizontally scrollable items.
Unfortunately the tvOS always focuses the UITableViewCell, not the UICollectionViewCell inside. I've already read about the
override var preferredFocusedView: UIView? {
return aView
}
So using this inside my controller is kind of hard regarding getting the correct subview.
Is there anything I could look into or could someone please point me in the right direction? Iterating through all subviews until I get the correct one seems like a bad idea.
Thanks a lot!
I tried out a similar situation where I had a CollectionView within a CollectionView to get the setup you described. Let's call them innerCV and outerCV. To get innerCV to be focused and not outerCV's cells, I set this delegate method to return false for outerCV.
- (BOOL)collectionView:(UICollectionView *)collectionView canFocusItemAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
In turn, innerCV's cells are now focusable as opposed to outerCV's cells.
Credit for this goes to Apple. I used their sample project as a guide to start. Check out the Focus samples within their project.
Sample Project: Apple tvOS UIKitCatalog
I used a different approach..
Setting the tableview selection property to No Selection in storyboard makes the focus engine to focus on UICollectionViewCell cell rather than UITableViewCell.
Hope it helps :)
Image
I'm getting stack in the design UI for an news application, i just want to make a transparent image under the 1st cell in a UITableView, and the Bar Menu on the top also transparent, which control they used for this one, like the below image
http://i.stack.imgur.com/C1C8N.png
Could anyone help me....
Thanks alot!!
I think it is a UITableView, where the first cell is customized in order to contain the large image. Then, they might be using a UIScrollView as the view for the first section, or a custom header view for the whole table - depends if that top stories "scroller" scrolls together with the rest of the talbe.
The rest is a "traditional" combination of a tab bar and a navigation controller.