I created collectionView as below:
I would like to create a collection view as same as this layout, with one biggest header cell like the one with red iPhone7. Now I wonder which approach is better, to create an extra cell or handle this in UICollectionViewFlowLayout?. My data is an array which fetched from JSON, so i would like to make the big cell is the first item.
Honestly, I see that second approach is quite complicated.
So can anyone help me to find best solution for this.
Thank you so much for reading this and have a nice day ahead.
Go to attribute inspector of UIColectionView.
From Accessories select Section Header
Once header view is visible , add your views to it.
Then in following method tweak accordingly:
override func collectionView(_ collectionView: UICollectionView ,viewForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader{
return headerView
}
}
Related
I am trying to animate the first apparition of a cell in a collection view as simple as a ZoomOut + Fade IN of the cells when the collection view is first called.
I have NOT subclassed UICollectionViewFlowLayout so far as I did not think it was necessary for such small action and I do not need to further invalidate the layouts once the cells are displayed. There is actually no animations of the cell beyond their first apparition. Reading the apple's developper guidelines on the subject seems to confirm I should not go to the subclass route but still I have not found how to perform the animation I want.
I have played along the lines of defining the cells using UICollectionViewDelegateFlowLayout methods then performing a CGAffineTransform sequence in collectionView(_ collectionView: willDisplay cell: forItemAt indexPath: IndexPath) of UICollectionViewDelegate and the proper transformation does occur. Then I was thinking of reversing this transformation by calling a simple CGAffineTransform.Identity() in an animation but I have not found yet where (in which of the CollectionView classes method) to place it.
Thank you in advance for your king help!
I am making a layout similar to this
Here all cells are different UI (approx 9 cells). So I tried using XIB files for each one and added in
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
}
But using xib i have achieved UI but i am finding it complicated.
Please guide me how can i achieve this layout, if there is any better solution than XIB.
And if any of this cell has to be reused in Other ViewController, how to design and code it to avoid repeating.
What you could do is make different cells and then when you call the cellForRowAtIndexPath you can load in a different cell depending on the index you are at. For example, at index 3 you may want to load in cell A while at index 5-7 you may want cell C.
Just go to the tableview drop in the cells you need, design each one, link it with a class, and then set its reuse identifier. Don't add them as section headers but as prototype cells.
What is the approach taken to incorporate table view with dynamic content as well as static table cells, like the following views (Any sample code is appreciated but not necessary). For the sake of learning, I would like to hear how you would go about constructing each of the following screens:
As far as possible, you shouldn't use static cells and dynamic cells together. It's a bad practice. This is because, when you use static cells, the dataSource for the tableView is set to be static, and cannot change at all.
But, there is a hack to this. And, you can use both static and dynamic cells together. But, you have to be really careful at doing this, otherwise your app can crash.
ToDo List:
1. Override the
cellForRowAtIndexPath method.
func tableView(_ tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// For static section i.e. 0 index
if indexPath.section == 0 {
// This returns the static cells
return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
} else {
// This gives your dynamic cell
let cell = myDynamicCell()
return myDynamicCell
}
}
Here, the myDynamicCell() method is your own method to create your dynamic cells. You cannot use prototype cell at this point, because you chose static cells already. So, you have to create the cells through code.
Note:
Keep in mind you are messing with the static dataSource. If any errors come at this point, it is your own responsibility as Apple states.
Also, this process is really dangerous, try to use dynamic cells and then set the first section dynamically to static. And, rest would be easy. This is the best approach to take. I would recommend this approach, but above approach would still work for this time, at least at the time of creating new cells.
I am stuck...
I have a collectionView (let's call it masterCollectionView). Each cell of the collectionView holds a list of data.
In each of these collectionView cells in the masterCollectionView, I have a tableView which holds all the records themselves in each row, grouped into tableView sections based on certain criteria within the dataset.
The data is stored in a multi-dimensional array which I download from the cloud, and this provides the list title to the masterCollectionView cell, as well as the # of sections, # of records (i.e. rows) in each list for the respective tableView.
My problem is that I can't tell which masterCollectionView cell I'm in when I'm populating the tableView. For example:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
The above function doesn't allow me to identify the masterCollectionView cell I'm currently building the tableView in and therefore I don't know which records in the data array to load.
At the moment I have all of the above built as one storyboard in interface builder along with one corresponding viewController but can easily change based on recommendations.
Please help. I'm pulling my hair out.
You probably want to have a separate instance of your table view data source for each collection view cell.
You could create a new NSObject subclass implementing UITableViewDataSource whose purpose is only to act as the data source for one cell. This object can be configured with the data for that cell only.
You would then have an array of these instances, one for each collection view cell. When the cell is loaded in cellForItemAtIndexPath, set that cell's table view delegate to the correct data source from the array.
Ok I will first give you the quickest solution to your problem, although its easy to implement it is not the 'right' way to do it I will explain later.
So the quickest way to do it is using view tags. Each UIView has a tag member which is an Integer. So where ever you are setting your tables, you can do this
I dont have your code so I am assuming you setting your table in
func collectionView(_ collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
function of your UICollectionView, so in there you do something like this
myTableView.tag = indexPath.row
This should set tag to the TableView, when you are creating them which would be index in the array. and in your UITableView datasource you can do this
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourDataArray[tableView.tag].count;
}
This is the short and quick way the correct way would be to break up your data properly, may your UICollectionView cells as datasources for the included UITableView and then load the data when needed.
I have an image inside my tableview cell.
What I want to do is when I click on that cell I want to parse both text & image to detail view.
I can do for text but I can't get the point about image.
So, simply my question is how can I parse image inside tableview cell to detail view in iOS.
Thanks
You can use it as the same way you're using it. Use indexPath to specify the row you wanna manipulate.
Here's how you do it. implement UITableViewDelegate in your ViewController, and implement the following method.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
The short answer, you access the image the same way that you set it. In the case of the standard UITableViewCell, it is the imageView property that you used.
For these API type questions though, there is no better place to start, than the documentation