Is it possible to disable floating headers in UITableView with UITableViewStylePlain?

前端 未结 29 986
走了就别回头了
走了就别回头了 2020-11-29 15:02

I\'m using a UITableView to layout content \'pages\'. I\'m using the headers of the table view to layout certain images etc. and I\'d prefer it if they didn\'t

相关标签:
29条回答
  • 2020-11-29 15:15

    The easiest way to get what you want is set your table style as UITableViewStyleGrouped, separator style as UITableViewCellSeparatorStyleNone:

    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
        return CGFLOAT_MIN; // return 0.01f; would work same 
    }
    
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        return [[UIView alloc] initWithFrame:CGRectZero];
    }
    

    Do not try return footer view as nil, don't forget set header height and header view, after you must get what you desired.

    0 讨论(0)
  • 2020-11-29 15:15

    This can be achieved by assigning the header view manually in the UITableViewController's viewDidLoad method instead of using the delegate's viewForHeaderInSection and heightForHeaderInSection. For example in your subclass of UITableViewController, you can do something like this:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UILabel *headerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];
        [headerView setBackgroundColor:[UIColor magentaColor]];
        [headerView setTextAlignment:NSTextAlignmentCenter];
        [headerView setText:@"Hello World"];
        [[self tableView] setTableHeaderView:headerView];
    }
    

    The header view will then disappear when the user scrolls. I don't know why this works like this, but it seems to achieve what you're looking to do.

    0 讨论(0)
  • 2020-11-29 15:17

    According to @samvermette's answer,I've implemented the above code in Swift to make it easy for coders to use swift.

    let dummyViewHeight = CGFloat(40)
    self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: dummyViewHeight))
    self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0)
    
    0 讨论(0)
  • 2020-11-29 15:18

    The interesting thing about UITableViewStyleGrouped is that the tableView adds the style to the cells and not to the TableView.

    The style is added as backgroundView to the cells as a class called UIGroupTableViewCellBackground which handles drawing different background according to the position of the cell in the section.

    So a very simple solution will be to use UITableViewStyleGrouped, set the backgroundColor of the table to clearColor, and simply replace the backgroundView of the cell in cellForRow:

    cell.backgroundView = [[[UIView alloc] initWithFrame:cell.bounds] autorelease];
    
    0 讨论(0)
  • 2020-11-29 15:19

    A tricky way is add an empty section for header. Because section has no cell, it will not floating at all.

    0 讨论(0)
  • 2020-11-29 15:20

    A probably easier way to achieve this:

    Objective-C:

    CGFloat dummyViewHeight = 40;
    UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, dummyViewHeight)];
    self.tableView.tableHeaderView = dummyView;
    self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0);
    

    Swift:

    let dummyViewHeight = CGFloat(40)
    self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: dummyViewHeight))
    self.tableView.contentInset = UIEdgeInsetsMake(-dummyViewHeight, 0, 0, 0)
    

    Section headers will now scroll just like any regular cell.

    0 讨论(0)
提交回复
热议问题