Jones
Jones

Reputation: 375

How to hide empty UITablecells?

How can i hide empty UITableCells? My Tablecell contains a background so i tried this line of code

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

But it didn't work (because of my uitablecell background)

Background:

[tabelView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"table.png"]]];

Upvotes: 1

Views: 311

Answers (3)

lukszar
lukszar

Reputation: 1422

Version in Swift 2.0

If you want tableViewController shows only defined cells, you have to implement delegate method tableView(tableView:, viewForFooterInSection section:) -> UIView? with empty UIView with clear color background.

In that case if you defined method tableView(tableView: UITableView, numberOfRowsInSection section: Int) to return 3 cells there will be ONLY 3 visible cells without separators or any background on another cells.

Here you have my example implementation of viewForFooterInSection:

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

        let view = UIView()
        view.backgroundColor = UIColor.clearColor()

        return view
    }

Upvotes: 0

Lvsti
Lvsti

Reputation: 1525

If you want to remove the dummy separators, you have to set the table view separator style to none, like this:

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

Note that this would hide the separator between non-empty cells as well. If you want to show the line between those, you have to take care of that yourself e.g. by using a custom UITableViewCell with a 1px line at the top/bottom.

Upvotes: 1

mschnueriger
mschnueriger

Reputation: 1

Do you want to hide the footer or the cells in the section?

If you have multiple sections you can check in which section the delegate actually is.

If (section == 0) {
Return view;
}

Upvotes: 0

Related Questions