0

I follow here to add UITableView header, just create a View which include two buttons, however, cannot see two buttons when I run the app.

two overriden functions are:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *vw = [tableView dequeueReusableCellWithIdentifier:@"HeaderCell"];

    return vw;
}

a screenshot attached: enter image description here

Note: the target ios: 11.4, xcode is 9.4.1, objective-c

========================================= rest code of ViewController.m

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithStyle:UITableViewStylePlain];

    if (self) {
        for (int i = 0; i < 5; i++) {
            [[BNRItemStore sharedStore] createItem];
        }
    }

    return self;
}

- (IBAction)addNewItem:(id)sender
{    
}

- (IBAction)toggleEditingMode:(id)sender
{
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];

    BNRItem *item = [BNRItemStore sharedStore].allItems[indexPath.row];

    cell.textLabel.text = item.description;

    return cell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];

    //[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"HeaderCell"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [BNRItemStore sharedStore].allItems.count;
}
James Hao
  • 765
  • 2
  • 11
  • 40

2 Answers2

0

Instead of

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *vw = [tableView dequeueReusableCellWithIdentifier:@"HeaderCell"];

    return vw;
} 

Write this:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewCell *vw = [tableView dequeueReusableCellWithIdentifier:@"HeaderCell"];

    return vw;
} 

dequeuResuableCellWithIdentifier returns a UITableViewCell as per the documentation:

@available(iOS 6.0, *)

open func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell

iPeter
  • 1,330
  • 10
  • 26
0

I have gone through the previous comment. You can try following

UITableViewCell * vw =[tableView dequeueReusableCellWithIdentifier:@"HeaderCell"];

    if (cell==nil) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HeaderCell"];
    }

And check the value in vw.

Himan Dhawan
  • 894
  • 5
  • 23