UITableView height for dynamic cells - ios

In my current project i'm trying to display data in a table View, however the former view does not take up the whole screen just a part of it. It's number of cells depends on an external json and the height of the cell is dynamic. Sadly there is no inbuilt way to use auto-layout(at least i think there's not) to expand the tableView depending on it's cells.
In the following picture there's an example of how one of these table views should look like:
And here's an example how the table view looks like using the autolayout:
Is there a way to expand the tableView to it's full size?
I've tried to follow various other questions but none have fully helped me, for exampled i've tried to this solution which sadly did not help me :[, creating a subclass of uitableview and overriding its intrinsicContentSize did not do the trick either.
Any help is much appreciated.
-Jorge :]

Related

Horizontally Scrolling UICollectionView with Vertically Scrolling Sections

I'm trying to implement the following:
At the top is a segment control that, based on the user's selection (either tapping or scrolling), moves to the relevant CollectionView section.
I've been playing around for a couple of days with CollectionViews and maybe I've just confused myself with LayoutFlows and CompositionalLayouts or is that over complicating it?
I have a diffable datasource that is made up of [Sections, CellItems] but I'm struggling with how to structure it.
What is the recommended programmatic (rather than using interface builder) to do the above? I was just going to originally do 5 horizontal cells, each with a tableView but based on some other comments I've seen from Apple on the Apple forums and the move away from TableView to CollectionView at this year's WWDC, I thought I should be trying to do this properly.
Any direction would be appreciated. All my research seems to point to using orthogonalScrollingBehavior but that layouts out the sections vertically with each section scrolling horizontally.
So my question is should I be trying to do what I'm doing using a CompositionalLayout?
Well first off your segmented control idea makes this sound like you could just use a page control for the horizontal aspect and remove a lot of the collection view complexity. Something to consider anyway.
As for the data source, I think all you need is an array of arrays. Eg. [[CellItems]]. So CellItems[0] would be the first column. Then CellItems[0][0] would be the first cell. CellItems[1][2]... 2nd column 3rd cell. I do apologize as your current solution may be related to diffable datasources and I haven't had time yet to dig into those.
Start with the horizontal axis first and ignore the vertical axis. Get that working as needed. Once you have that up and running... implement another collection view into the first one's cells.
Sorry if I misunderstood the question if it was more about HOW to implement a collection view.

How to create table view with rounded corners around the list items

I am trying to recreate a menu similar to the ones in the detail view of the iOS 13 Health app. Please refer to the marked up screenshot.
I know that this can be done with a table view. There's a section title and list items. But what I want to achieve is similar to the look shown in the screenshot whereby there's a background colour on the list items (not including the section title) and rounded corners at the top and bottom of the list.
Can anybody tell me how I can achieve this with the table view? Or point me towards the right direction? I already know how to setup table views and programmatically add the details. I just need help on how to achieve the styling as shown below.
Thanks!
So, I was able to figure it out. For the benefit of the devs who stumble in the same dilemma, I'm posting my solution here. But I will be tagging Glenn's answer above as the correct answer as it lead me to find the solution. Thanks again Glenn!
It appears that I didn't have to do anything special with my code. I just discovered that on XCode 11 and iOS 13, there's a new table view style called "Inset Grouped". You may set this property from IB or via code.
With a quick experimentation I was able to come up with the result as shown on the screenshot below.
It's not that complex to do. This one of the multiple ways that that style can be done.
a. You can use grouped tableView, as what you've indicated in the screenshot.
b. Provide section title, or better yet, a section view (for more customization!).
c. For each section, you have ONE tableViewCell.
d. For each cell of that c., you will have a tableView.
e. For each tableView of that d., you will have your a new cell of course (item cell).
f. How to compute for the height of the tableView of e.? There are multiple ways.
Provide static height (if your number of items are static).
If dynamic count, but you have constant height of each cell, then you can just compute it like so: itemsCount * heightConstantOfCell
If again you have dynamic count of rows/items, and you have iether constant height of each cell or dynamic height of each cell, then you can observe the frame key of the whole tableView.
g. Finally, just add some corner radius to each container view of your tableView in d..
Note, this screenshot ONLY shows the item g.. It's merely a corner radius of each container view of your tableView in a tableViewCell that is a cell of your main tableView.
Another way is to use UICollectionView, but kinda more complex than what I've discussed - at least for me.

How create a tableView that covers an image when it is scrolled

I'm trying to develop something like CSStickyHeaderFlowLayout but customized for my table, but I'm not sure how can I achieve this goal. The idea is
Someone can give me a hint how achieve this objective?
To add to Vollan's answer, to make the title stay still you could use a view that contains two subviews: the first is the scrollview (with the image and table as Vollan suggests) and then add another view (like a UILabel) for the title. Thus, while the image and table scroll in the scrollview, the title will stay still.
Best solution would be to wrap everything inside an UIScrollView. That will allow you to scroll to bottom of the screen and then only scroll the tableview. That way it will appear like the tableview will overlay the image.
While using a tableview within a scrollview would likely work, your tableview would have to always be it's full size (without some annoying constant re-sizing), so you'll lose the value of the enqueuing/dequeueing that makes tableViews work so well.
The CSStickyHeaderFlowLayout has example pretty similar to what you want to do, did you look at their examples? You may be able to play with it and get it to do what you want If your problem is simply having a constant title, you can just add a view above the table or use the NavBar and adjust the contentInsets
You might also consider using a collectionView instead. It's much more flexible as far as layout goes.

What's the best way to implement a vertical timeline - iOS

First of all, apologies if this question isn't meant here. I searched a lot but didn't find anything.
What is the best approach to create a vertical timeline kind of view?
I tried implementing via UITableView by alternating cells but that approach is very messy in terms of autolayouting (had to disbale autolayout and apply programtically when creating cell on alternate side), thereofre isn't working properly (getting stuck while scrolling). This is what I achieved:
I want to achieve something like this, which is also easily manageble:
Please Suggest. Thanks!
I would have 1 cell row with two image views. One on the left, one on the right. I think this would be easier to use auto layout.
Then you can hide the image view and labels that shouldn't be used.

How to enable scroll in UICollectionView?

I’m still learning to develop in iOS and Swift so I apologize beforehand if my question is too simple.
I have created an UICollectionViewController to show a matrix of elements. My issue is that the width of the screen is not wide enough to fit all the columns in a single row, so the excess of them are shown in another row below. Instead of this, I’d like to enable the horizontal scroll so the user can scroll to see all the columns in one single row.
I know UICollectionViewController already contains UIScrollView so I guess this should be as simple as to change a setting in the .storyboard but I couldn’t find it after many trial-errors :(
I guess it is related to the "Flow" layouts setting and that I need to handle a custom one, but don't know how exactly.
Would anyone be so kind to please help me? I haven’t attached any code because I’m using a pretty standard/out-of-the-box implementation of UICollectionViewController but if you want me to add anything, just let me know, please.
Many thanks in advance for any help!
I found the answer to my issue perfectly covered on this post:
https://www.credera.com/blog/mobile-applications-and-web/building-a-multi-directional-uicollectionview-in-swift/
It's definitely to do with your UICollectionViewLayout — that tells the collection view where everything should go. The collection view simply picks its scroll behaviour appropriately.
A UICollectionViewFlowLayout is a specific type of layout that fills one row column to the size of the enclosing view, then moves on to the next. So it does one-dimensional scrolling. Does that fit your use case?
If so then you should just be able to set the scroll direction on the flow layout.
If not then you'll need to write a custom subclass of UICollectionViewLayout that implements the layout behaviour you want.

Resources