I'm new to IOS development with swift and I'm having a problem. I need to create a tableview and it looks almost the way I wanted, except for the space at the top of the first section of the table. It has no name but I would like to reduce the space between the top and the first item. What I was able to do is according to the code and image below:
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch (section) {
case 0:
return ""
default:
return self.nameSection2
}
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.white
let headerLabel = UILabel(frame: CGRect(x: 15, y: 8, width:
tableView.bounds.size.width, height: tableView.bounds.size.height))
headerLabel.font = UIFont(name: "Verdana", size: 16)
headerLabel.textColor = UIColor.lightGray
headerLabel.text = self.tableView(self.tableView, titleForHeaderInSection: section)
headerLabel.sizeToFit()
headerView.addSubview(headerLabel)
return headerView
}
The 'margin' you see is because the height for both the section headers is the same. The second one looks less-empty as it actually has a title.
You can modify the height for the headers to reduce the space:
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
switch section {
case 0:
return 0
case 1:
return 44 //Required height value here
default:
return defaultValue //Any default value
}
}
You need to implement the heightForHeaderInSection so you can collapse that header. See below:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 1.0
} else {
return 32.0
}
}
You can set the appropriate value for the else condition for your needs but this gives you the idea.
Update #1: I found this link in searching that may help as well: http://stackoverflow.com/a/23955420/3965
It recommends using GLFloat's minimum value instead:
if section == 0 {
return CGFloat.leastNormalMagnitude
}
return tableView.sectionHeaderHeight
Implement heightForHeaderInSection and return the height you want for the first section.
Also, you wouldn't normally implement titleForHeaderInSection and viewForHeaderInSection. Just put your switch statement in viewForHeaderInSection to set the text for your label.
And you don't need to put your UILabel into headerView, just return the label. Or instead of UIView, use UITableViewHeaderFooterView.
In my app I have a UITableView with 5 cell data. When I will run the app. It will display 5 cells with data properly in Output. But there are many extra cell show empty. I want to remove extra cell in UITableView. I have also done in objective-C but I need some help in Swift 2.2 also I'm new in swift.
in your viewDidLoad() add this line :
yourtableView.tableFooterView = UIView()
or use
yourtableView.tableFooterView = UIView(frame: CGRectZero)
Try the following code. May be this is helpful to you.
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
The issue might be arising from the fact that you are specifying a greater number of rows in the UITableView than required to populate data. Assuming you are populating the UITableView from an Array, you should probably set the row count to the count of array.
If you are using a UITableViewController override this method
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourArray.count
}
If a UITableView is embedded in the UIViewController set UITableView delegate to self and implement this method.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return yourArray.count
}
write this code
self.tableView.tableFooterView = UIView(frame: CGRectZero)
Implement this delegate method like this:
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.1
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
I would like to have a header section in a table view with a logo and name. Now, I want the logo to overlap the image in the content cell below. I have attached an example of this below :-
Heres what I've tried so far, I've tried to make the header section half the size of (logo + upper and lower padding) -- this just cuts the logo in half
I also tried clipToBounds :-
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 33.0
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! FeedTableViewHeaderCell
cell.designerNameLabel.text = "Vitamin A"
cell.designerLogoImageView.image = UIImage(named: "zeko_small")
cell.designerLogoImageView.clipsToBounds = false
cell.contentView.clipsToBounds = false
return cell
}
You may set ImageViewFrameHeight greater than TableViewHeaderHeight and In your TableHeaderViewCell, set
TableHeaderViewCell.contentView.clipsToSubview = NO.
This'll definitely help you in getting your result.
Visual guide from XCode 7:
Please put this method and set your section's header height as you want.
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 100// return height which is greater than your image's height.
}
I have a UITableView which contains a bunch of cells (dynamically generated) and it displays a footer message showing the last updated time. The issue I'm having is that on the first loading of the UITableView it seems to display the footer over the top of the last cell. Once you reload the data it displays it correctly though.
Does anybody know why this is happening?
Initial loading:
After reloading:
The following is the logic I use for establishing the footer message to display:
override func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
var title = ""
if (section == kSectionA) {
if (!self.hasInternet) {
title = "No Internet Connection"
} else {
if (self.dataToDisplay) {
title = "Last Updated: xx:xx AM"
} else {
title = "No results found."
}
}
}
return title
}
You need to tell the Tableview how big the footer is from the beginning.Try using this code but with your values.
Tells the Tableview how big the footer is:
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
footerView.backgroundColor = UIColor.grayColor()
return footerView
}
Tells the footer how big the footer is:
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40.0
}
That should solve you problem.
I need to reduce the space between two sections ofUITableView. I looked at this question but the solution doesn't allow for my custom header view because it combines the space of the footer and header.
Here is a picture of the UITableView. The black color is the UITableView background color.
On iOS 15 you may want to reduce the sectionHeaderTopPadding
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
}
Did you try override this function:
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}
I think you can solve this by adjusting the footer view height to its min: in Storyboard or XIB.
I don't know what you have written in your code for footer height. Sorry if I am wrong.
Possible duplicate of Hide footer view in UITableView
For Swift 4+ you need to implement these two methods
extension MyViewController : UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
}
For Swift 3
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
For Swift 5+:
There is some space for the headers and footers by default. That's why I was having the problem of setting an exact separation for the sections.
My solution to having a separation between 2 sections is the following:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 24
} else if section == 1 {
return 32
} else {
return 40
}
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
nil
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
nil
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
CGFloat.leastNormalMagnitude
}
As you see for viewForFooterInSection and viewForHeaderInSection I needed to return nil.
In case you only want to change the footerHeight, just return CGFloat.leastNormalMagnitude for heightForHeaderInSection, and return the heights for each section in heightForFooterInSection.
Along with the answer posted by Icaro I would like to add that you also need to implement the tableView:viewForFooterInSection: method returning nil for the section you want to remove the empty space below
It will then become:
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return nil;
}
You need to use the method heightForHeaderInSection for defining the space between header & cell text. You can also change it depending on different sections for eg. at some sections you may need to show more distance & under some, you don't want to show gap. For such case you can use CGFLOAT_MIN which is 0.000001f. Giving you an example, how you can use different section with different header heights:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0 || section == 2)
{
return 55.0;
}
else
{
return CGFLOAT_MIN;
}
}
This also may help :
override func viewDidLoad() {
super.viewDidLoad()
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
}
Select the tableView in your storyboard/objectCode and ensure that the style is set to Plain, instead of Grouped. You can find this setting in the attributes "Inspector" tab.
let myTableView : UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.register(TableCellClass.self, forCellReuseIdentifier: "cellId")
tableView.backgroundColor = UIColor(red: 123/255, green: 190/255, blue: 120/255, alpha: 1)
tableView.separatorStyle = .none
tableView.translatesAutoresizingMaskIntoConstraints = false
return tableView
}()
In Xcode 13.2, you can adjust the height of the header and footer of sections in the storyboard - see screenshot below:
TableView Delegate methods doesn't effect with float value is 0.0f. Try giving a value greater than that.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.00001f;
}
- (UIView*)tableView:(UITableView*)tableView
viewForFooterInSection:(NSInteger)section {
return [[UIView alloc] initWithFrame:CGRectZero];
}
Rather than implementing the UITableViewDelegate methods and defining the sectionFooterHeight via CGFloat.leastNormalMagnitude, one can alternatively just
tableView.sectionFooterHeight = 0
and the spacing between sections while no footer is present will go away.
The mechanism is that by default this value is set to UITableView.automaticDimension.
As long as
it stays UITableView.automaticDimension
there are no delegate/dataSource methods that implement the configuration of footer i.e. titleForFooterInSection/viewForFooterInSection
table view's style is set to .grouped
then UITableView will deliberately insert a spacing between sections with no view.
You change sectionFooterHeight to 0, the magic goes away.
I just simply had to reduce the top padding for the tableview section header:
tableView.sectionHeaderTopPadding = 0
You can do it by implement the delegate heightForHeaderInSection & heightForFooterInSection.
The return vaule should not be 0, even if the SectionHeader or the height of SectionFooter is 0, it need a very small value, try CGFLOAT_MIN.
for my example:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == [self.dataArray indexOfObject:self.bannerList]) {
return 46;
}
return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
Work for me
tableView.sectionFooterHeight = 10
// ...
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}
swift 5 iOS 15
self.tableView.estimatedSectionFooterHeight = 16.0 // spacing between Sections