Display Separator only for the Available CellForRow in UITableView

前端 未结 3 1561
渐次进展
渐次进展 2020-12-31 09:37

I am using UITableView with custom cell.
It is working fine but problem is when there is only one or two cell in UITableView.
It is giving the separator for the e

相关标签:
3条回答
  • 2020-12-31 09:52

    Put this peace of code in your viewDidLoad

    self.tblTest.tableFooterView = [[UIView alloc] initWithFrame : CGRectZero];
    
    0 讨论(0)
  • 2020-12-31 09:57

    You need to add an empty footer view to hide empty rows from a table.

    Swift:

    In your viewDidLoad()

    self.tblPeopleList.tableFooterView = UIView.init()
    

    Objective-C:

    Easiest way:

    in your viewDidLoad method,

    self.yourTableView.tableFooterView = [[UIView alloc] initWithFrame : CGRectZero];
    

    or

    self.yourTableView.tableFooterView = [UIView new];
    

    or

    If you want to customize the look of footer view, you can do it like this.

    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    view.backgroundColor = [UIColor redColor];
    self.yourTableView.tableFooterView = view;
    
    //OR add an image in footer
    //UIImageView *imageView = [[UIImageView alloc] initWithImage:footerImage.png]
    //imageView.frame = table.frame;
    //self.yourTableView.tableFooterView = imageView;
    

    Another way:

    Implement data source method of a table,

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        return [UIView new];
    }
    

    It's the same thing, but here you can add different views for each section if the table has multiple sections. Even you can set different height of each section with this method, - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {...}.

    Objective-C Answer Note: This answer is tested for iOS7 and above, for the previous versions you've to test each case. Swift Answer Note: This answer is tested for iOS10.3 and above, for the previous versions you've to test each case.

    0 讨论(0)
  • 2020-12-31 10:00

    Another solution:

    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    v.backgroundColor = [UIColor clearColor];
    [self.tableView setTableFooterView:v];
    

    This works too

    self.tableView.tableFooterView = [UIView new];
    
    0 讨论(0)
提交回复
热议问题