UITableViews inside UIScrollView - ios

I have read a few posts asking about to put UITableViews inside a ScrollView but mostly this idea is not recommended. The "standard way" to handle this situation as I understand is to use just the UITableView instead of the ScrollView and make the TableView header the UIView to display the content and to use the cell to display any dynamic rows.
However the app I am now developing is a bit different: the first part of the view is some descriptive content that I can use the header view to display. But after that I want to display two scrollable sections potentially with dynamic height. One is a comment section displaying all comments and replies and another is a user section that display any users ever made an offer. This two sections need to be row-based and will be dynamic in height as the content inside them will be different.
So the question is, how should I implement this without putting two UITableViews inside a UIScrollView(My original plan)?

UITableViews inside a ScrollView is not at all recommended as UITableView as its own scrollView as subView.
This two sections need to be row-based and will be dynamic in height
as the content inside them will be different.
Whats wrong with using the delegate for dynamic height for row
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {}
Some Tutorial:
https://www.raywenderlich.com/129059/self-sizing-table-view-cells

Related

How to insert a random view beneath any UICollectionViewCell

Sorry for the influx of UI and GUI questions but I was recently looking at an application called Ganna and in this pic below I was wondering how you would get that random Ganna Around The Globe banner beneath the Recently Played horizontal collection view and above the Top Charts horizontal collection view.
The way it's organized is that there is a main collection view with multiple horizontal collection views such as the Recently Played and Top Charts horizontal collection view.
I am thinking if the indexPath is 0 (which means you are at the Recently Played horizontal collection view) then you would insert that Ganna Around the Globe banner underneath that collection view. But this would be difficult since I would have to return different heights of the cell depending on whether that banner exists or not. And this banner appears 3 times beneath 3 different horizontal collection views.
Do you guys have any ideas? Thank you!
I can't say if this is how that particular example is implemented. But the way I would approach this is fairly simple.
You would have a single TableView with dynamic cells that covers the entire screen from nav bar to tab bar. Make two custom cells, the first custom cell contains a collectionView. The second cell would be laid out in whatever manner you want for displaying that banner.
In your cellForRow method of the tableViewDelegate you set the delegate of the collectionView inside the cell to whatever it needs to be.
The only thing left after that is a tiny piece of logic in the heightForRow method. Something like
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (indexPath.row == kBannerCellIndex) {
return showBanner ? kBannerHeight : 0
}
// the rest of any height logic
return defaultCellHeight
}

Insets around section of UITableView

Is there a way to give spacing around a section? I know this can be done using UICollectionView. Have implemented all the things using UITableView and UITableViewCells, don't want to move things to UICollectionView.
I am trying to achieve space around the section(all sides) to give a group kind of feel for section(including header, cells and footer views).
You can use tableView(_ tableView: UITableView, viewForFooterInSection section: Int) and tableView(_ tableView: UITableView, heightForFooterInSection section: Int) to add more space between sections without messing with margins.
There is no straight forward api for this.
Add some space on every side of UI elements which you have added on top of section header view. eg. (10,10,width,height)
Apply same gap for Cell subviews as well.
If you created your table view section header via IB, you should add the one more UIView object before doing anything else. Then, you have to add correct constraints to this view relative to the header content view. I think you will add your margins from each side. Then you should adjust your table view background colour. Now I think you are free to make something like you described
You could actually use the tableView separator inset, with the separator style of none, and That's it!
I know it's a bit late but hopefully the answer could be of use for others.

Swift: How to create space around cells in a static UITableView

I am trying to create a gap between my cells in a static UITableView. The UITableView has 3 sections, each containing 2 rows. I have tried
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 15
}
but this method does not seem to work.
This might be a little bit of a hack but it has worked for me in the past.
Simply add a UIView to the UITableViewCell and set it as a different background colour to the UITableView.
If you make the UIView smaller than the UITableViewCell, you will be able to get the desired spacing just by adding constraints.
I hope I've understood the correctly
This isn't something that UITableView does by default. There isn't a simple way to add spacing between cells, that's generally something you'd accomplish by using a UICollectionView.
If you have to use a table view you can try adding the padding into a custom UITableViewCell itself, which will create the affect you're looking for. You could also try adding additional "spacer" cells that go in between your primary content cells.

How to correctly modify iOS table view cells at runtime (add/remove subviews?)

Apologies in advance if this has been answered somewhere. I've looked everywhere and I'm still unsure what to do. (And answers that use Objective C are almost completely worthless to me.) I'm somewhat new to iOS.
I have a UITableView which serves as a newsfeed of sorts, displaying a series of posts. (a la a twitter newsfeed, for instance.) The cells in this table view are (currently) derived from a single cell prototype, created in xcode's interface builder. Each cell contains subviews to display things like username, profile image thumbnail, title, message, date, location, another image, etc.
The problem is, depending on what data a particular post contains, many of these subviews either should or should not be shown -- if a post doesn't contain an image, then that cell's image view should not be shown; if a post doesn't have a date and/or location, one or both of these views should not be shown. Not only should the unused fields be empty, but they shouldn't take up any space in the cell.
I read in Using Auto Layout in UITableView for dynamic cell layouts & variable row heights (under "2. Determine Unique Table View Cell Reuse Identifiers". Wonderful answer by #smileyborg, btw.) that for each different layout of subviews that could be in a cell, a different prototype cell and reuse identifier should be used. But this would require me to have a cell prototype for every single possible combination of data items in a post, even if the difference is single label! Surely there must be a better way.
What is the safe and correct way to do what I need to do? Is there perhaps a way to remove subviews from cells at runtime (and have the layout adjust its spacing accordingly) without completely screwing up cell recycling?
Ill assume you know everything you want to know about your layout when you see the cell.
So its basic tableview cell layout. Dequeue and decorate.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(MyIdentifier, forIndexPath: indexPath) as! MyCustomCellClass
let data:MyDataClass = myDataAtIndexPath(indexPath)
decorateCell(cell,data:data)
return cell
}
func decorateCell(cell:MyCustomCellClass,data:MyDataClass) {
//here is where you arrange/change your constraints and hide/reveal views
//depending on the data
}
but also what you need is to allow the cell to set its height properly
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return someEstimatedNumberWhichIsClose;
}
but most crucially in your XIB, the cell MUST have a continuous line of constraints from top to bottom which describe the height.
So this cell will auto-set its height correctly.
While this one will not.
NB - in the upper example , if you leave the height constraint off the text-field and make it infinite line count the cell will grow/shrink appropriately.
So to summarise you can use one multipurpose cell as long as you keep your height constraints coherent.
As a general rule - Swiss Army CellsĀ® which do lots of things may get a bit unwieldy so you do need think about how many use cases you wish to support with one cell and then maybe start creating multiple types/identifiers.

iOS: Hide all lines for specific section in UITableView

I have UITableView with two sections, and I want the first section to appear without lines.
is it possible to hide all lines (top and bottom lines and cell separators) for only one section ?
No, separatorStyle is a property of UITableView, so there is no way to change it for specific sections / rows.
But it's not hard to implement yourself, with a custom UITableViewCell subclass

Resources