how to hide empty rows in a UITableView and change the height of the Uitableview based on non-empty rows
IosUitableviewIos Problem Overview
I have couple of problems with my UITableView
.
-
When I add a
UITableview
on my page, by default it brings up some fixed number of rows, even though I set number of rows in section as1
. All the rows appear except the first row, and all are empty rows. So, I want to hide all the empty rows in theUItableview
. -
Based on the non-empty rows, I want to change the height of my
UItableView
.
Ios Solutions
Solution 1 - Ios
NEW ANSWER
In Swift 2.2, 3.0 and onwards, do the following:
OLD ANSWER BELOW. KEPT FOR POSTERITY.
If you must use UITableViewStylePlain
, and you don't use a footerView for anything else, you can use the following semi-dirty solution if you have ARC enabled.:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
return view;
}
If you have ARC disabled, use the following:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[[UIView alloc] init] autorelease];
return view;
}
This creates an invisible footerView, that appears immediately after the last data-filled cell.
Solution 2 - Ios
You sure you are using UITableViewStyleGrouped
style?
Because by default it is set to UITableViewStylePlain
which shows empty cells too after displaying filled cells.
Solution 3 - Ios
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
}
and for iOS 7
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Solution 4 - Ios
I solved the problem by creating a simple UIView as footer view with the same background color as the table background:
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
view.backgroundColor = [UIColor whiteColor];
return view;
}
Maybe you have to disable scrolling in your table view in addition.
Solution 5 - Ios
If your tableview has multiple sections, you may try using this:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return [UIView new];
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return 1;
}
return 0;
}
Solution 6 - Ios
- (void) viewDidLoad
{
[super viewDidLoad];
self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
}
Solution 7 - Ios
This can also be done through Interface Builder. Simply add a 1 pixel tall UIView as a footer to the UITableView. It's essentially the same as most of the answers here, but it keeps some UI specifics in the view instead of in the view controller.
Solution 8 - Ios
[self.SomeTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Use this to get rid of the lines if the UITableView
is empty.
Solution 9 - Ios
in the method that returns the number of rows, specify the count
yourself dynamically. For example if you are going to populate the table with say a NSArray
named arrayForRows:
return [arrayForRows count]; // inside the method which returns the number of rows.
The above is a simple example populating a table with an array as a datasource. There are no empty rows. Only that many rows show up in the table according to the count
of items in the array populating the table.
I think you mean height of the row in the table. You can play around with the height of the rows by using the method:
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
EDIT: ah now I get what you are trying to achieve. You are trying to avoid showing the empty rows separated with those separator lines right? See this post:
https://stackoverflow.com/questions/1491033/how-to-display-a-table-with-zero-rows-in-uitableview
Solution 10 - Ios
A bit hidden in Bourne's answer; if you want to hide to bottom empty rows in a plain tableview with multiple sections, use this answer:
Solution 11 - Ios
For Swift:
override func viewWillAppear(animated: Bool) {
self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)
// OR
self.tableView.tableFooterView = UIView()
}
For C#: (Please don't forget to add using CoreGraphics;
)
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.sampleTableview.TableFooterView = new UIView(frame: CGRect.Empty);
}