How to Remove Empty Cells in Uitableview

How to remove empty cells in UITableView?

Set a zero height table footer view (perhaps in your viewDidLoad method), like so:

Swift:

tableView.tableFooterView = UIView()

Objective-C:

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

Because the table thinks there is a footer to show, it doesn't display any cells beyond those you explicitly asked for.

Interface builder pro-tip:

If you are using a xib/Storyboard, you can just drag a UIView (with height 0pt) onto the bottom of the UITableView.

How to remove extra empty cells in TableViewController, iOS - Swift

in your viewDidLoad() function add this line of code:

tableView.tableFooterView = UIView()

how to hide empty rows in a UITableView and change the height of the Uitableview based on non-empty rows

You sure you are using UITableViewStyleGrouped style?

Because by default it is set to UITableViewStylePlain which shows empty cells too after displaying filled cells.

How do I remove empty cells in table view? iOS Swift

You need to filter out the ones that don't have a location

func filterList() { // should probably be called sort and not filter
contacts.sort() { $0.name < $1.name } // sort the fruit by name
contacts = contacts.filter { $0.location != nil }
//tableView.reloadData(); // notify the table view the data has changed
}

iOS 15: Remove empty space before cells in UITableView

Check if you are only seeing this issue on iOS 15. If so, this may be caused by the newly introduced UITableView.sectionHeaderTopPadding property. You will need to set this value to 0 in order to remove the spacing before section headings:

let tableView = UITableView()
tableView.sectionHeaderTopPadding = 0

// Etc.

This property is only available in iOS 15 so you will need an API check if building for earlier versions.

If you're not on iOS 15, this question has most of the answers to this issue.

Remove empty space before cells in UITableView

Do the cells of the UITableView show on the empty space when you scroll down?

If so, then the problem might be the inset that is added to the UITableView because of the Navigation controller you have in your view.
The inset is added to the table view in order for the content to be placed below the navigation bar when no scrolling has occurred. When the table is scrolled, the content scrolls and shows under a transparent navigation bar. This behavior is of course wanted only if the table view starts directly under the navigation bar, which is not the case here.

Another thing to note is that iOS adjusts the content inset only for the first view in the view hierarchy if it is UIScrollView or it's descendant (e.g. UITableView and UICollectionView). If your view hierarchy includes multiple scroll views, automaticallyAdjustsScrollViewInsets will make adjustments only to the first one.

Here's how to change this behavior:

a) Interface Builder

  • Select the view controller
  • Open Attributes inspector
  • There's a property called "Adjust scroll view insets" in IB's attribute inspector (when a view controller is selected) which is on by default. Uncheck this option:

    Sample Image

    (Image courtesy of Dheeraj D)

I'm not sure which Xcode version introduced this option (didn't spot it in the release notes), but it's at least available in version 5.1.1.

Edit: To avoid confusion, this was the third option mentioned in the comments

b) Programmatically

Add this to i.e. viewDidLoad (credits to Slavco Petkovski's answer and Cris R's comment)

// Objective-C
self.automaticallyAdjustsScrollViewInsets = NO;

// Swift
self.automaticallyAdjustsScrollViewInsets = false

c) This might be relevant for old schoolers

You can either fix this by adding

tableView.contentInset = UIEdgeInsetsZero

//Swift 3 Change
tableView.contentInset = UIEdgeInsets.zero

Or if you are using IB and if the navigation bar is not transparent (can't tell from the screenshot)

  • Select the view controller
  • Open Attributes inspector
  • In View Controller options Extend Edges section deselect "Under Top Bars"

UITableView leaves pattern of blank cells after deleting one or more rows

override func prepareForReuse() {
super.prepareForReuse() // <--
self.nameLabel.text = nil
self.backgroundColor = .white
}

Within my custom cell implementation, the above function was being called without calling super.prepareForReuse first. Therefore causing the issues above.



Related Topics



Leave a reply



Submit