I want to remove the top margin in Grouped UITableView (swift)

Xcode-12.0iOS-14.0Swift-5.3

Introduction

As shown in the image below, when you want to switch the display / non-display of the top margin depending on the condition in ʻUITableView where styleisgrouped`, you made a trial and error, so make a note of the method.

Margin display Hide margins
success_1 success_2

This time, I wanted to eliminate the margin when displaying the header like the image, and to have the margin when the header is not displayed. .. ..

Bad pattern

The pattern that was useless for the time being. .. .. As shown below, when ʻisHeaderShown is true`, I set the header in section 0 and set the height.

extension TableViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        guard section == 0 else {
            return UITableView.automaticDimension
        }
        return isHeaderShown ? 50 : UITableView.automaticDimension
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        guard section == 0 else {
            return nil
        }
        return isHeaderShown ? HeaderView() : nil
    }
}

result

failure

Only the first display is good, but the second and subsequent times are strange. .. .. : frowning2:

Pattern 1 that seems to be good

Above method + I tried to play with tableHeaderView when updating the table as shown below.

tableView.tableHeaderView = isHeaderShown ? UIView() : nil
tableView.reloadData()

result

success

Got it: tada:

Pattern 2 that seems to be good

It seems that the above method can be used, but if you set the header only at the top in the first place, I thought that it is not necessary to set View in sectionHeader, and finally I did as follows.

extension TableViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        guard section == 0 else {
            return UITableView.automaticDimension
        }
        return isHeaderShown ? CGFloat.leastNormalMagnitude : UITableView.automaticDimension
    }
}

//Processing at the time of update
tableView.tableHeaderView = isHeaderShown ? HeaderView(frame: .init(origin: .zero, size: .init(width: 0, height: 50))) : nil
tableView.reloadData()

The result was the same as pattern 1, so maybe it's okay: clap:

in conclusion

I think it's rare to want to have such a layout, but I hope it helps someone. If you know any other good way, please let me know: pray:

I've heard that it's better to use ʻUICollectionView rather than ʻUITableView in the future, and it may not be good to clutter the table too much anymore. .. .. : thinking:

Recommended Posts

I want to remove the top margin in Grouped UITableView (swift)
I want to get the value in Ruby
I want to embed any TraceId in the log
I want to return the scroll position of UITableView!
I want to set the conditions to be displayed in collection_check_boxes
I want to transition to the same screen in the saved state
I want to simplify the conditional if-else statement in Java
Swift: I want to chain arrays
I want to create a chat screen for the Swift chat app!
I want to display the images under assets/images in the production environment
[Java] I want to perform distinct with the key in the object
I want to change the value of Attribute in Selenium of Ruby
[Android] I want to get the listener from the button in ListView
Shorten the UUID to base64 in Swift.
I want to use @Autowired in Servlet
I want to write JSP in Emacs more easily than the default.
I want to get the IP address when connecting to Wi-Fi in Java
I want to display an error message when registering in the database
I want to output the day of the week
[Swift 5] Processing to close the keyboard on UITableView
[Swift] I want to draw grid lines (squares)
I want to send an email in Java.
I tried to organize the session in Rails
I want to use arrow notation in Ruby
How to add sound in the app (swift)
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
rsync4j --I want to touch rsync in Java.
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to truncate after the decimal point
You may not want to use the remove method in ArrayList very often
I was addicted to the NoSuchMethodError in Cloud Endpoints
I want to use Clojure's convenient functions in Kotlin
I tried to organize the cases used in programming
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
[Swift] I implemented a Twitter timeline in UITableView (Part 2)
Tokoro I rewrote in the migration from Wicket 7 to 8
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to judge the range using the monthly degree
[Swift] I implemented a Twitter timeline in UITableView (Part 1)
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to dark mode with the SWT app
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
I want to define a function in Rails Console
I want to place RadioButtons in the same RadioGroup at any position on the screen.
[Swift] I want to do something like C's sprinftf
[Rails] I want to display the link destination of link_to in a separate tab
[Swift] Use UserDefaults to save data in the app
I want to stop snake case in table definition
I want to call the main method using reflection
If you want to recreate the instance in cloud9
I want to click a GoogleMap pin in RSpec
# 1_JAVA I want to get the index number by specifying one character in the character string.
[Rough commentary] I want to marry the pluck method
I tried to implement the Euclidean algorithm in Java
I want to be aware of the contents of variables!
I want to simplify the log output on Android