I am trying to make an expandable tableview. Here I didn't use headers and made all of those section stuff using rows. In here I've found a bad behaviour that blocks my move operations. As you see in gif, i added height on footer sections to show what is really happening. I don't want any border in my application between sections. As you see, I want to move rows into upper section if it is upper than 'Done' section text. How can I achieve those purpose?
Problem Demo: https://i.imgur.com/VIMf5sb.gif
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
print(sourceIndexPath)
print(destinationIndexPath) }
In the buggy situation destinationIndexPath section is 2 and row is 0. So I believe my code does not contain any code error. How can I fix that UX problem when there is no border between sections?
Related
I know there are tons of questions about tableViewCell disappearing on scroll, but none of those fit into my situation.
I say disappear, I mean the whole cell disappears, not some of its subviews.
Here's my situation:
I have a tableView with custom cells, each one of them is very complex, some could even host up to three collectionViews in them. Most of these cells are only displayed once, so I cache them. I retrieve data from network, and use it to populate the cells. As sometimes the data may not be fully available, some sections of the cell needs to be hidden: the cell will set some of its collectionViews' height constraint to zero, and isHidden to true, then notify the tableView, who will reload the cells' heights using:
tableView.beginUpdate()
tableView.endUpdate()
I have noticed two behaviors:
1:
If none of the cell's portion is hidden, the cell works perfectly, except when I scroll(from bottom up) too fast, the cell disappears before going out of the bound of the tableView. When I scroll slow, this does not happen.
2:
If some part of the cell is hidden, the cell will disappear before going out of tableView EVEN WHEN I scroll very slowly.
Sorry I cannot post any code, but what I might have done wrong?
P.S.: In my tableView's row height delegate method, I have:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return cellHeights[indexPath.row] ?? 0.1
}
cellHeights is where I use a dictionary to store computed height of the corresponding cell. I am certain that these height values are correct, because when I scroll from top down, the cell looks all right. This only happens when I scroll from bottom up.
I did some experiment with the default height 0.1. When I changed it to 1000, the cells are disappearing randomly even more. 0.1 looks relatively stable, but the problems above still exist.
Thanks.
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
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
}
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.
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.