首页 > 解决方案 > 在 UITableView 标题上看不到按钮

问题描述

我按照这里添加 UITableView 标题,只需创建一个包含两个按钮的视图,但是,当我运行应用程序时看不到两个按钮。

两个覆盖函数是:

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

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

    return vw;
}

附上截图: 在此处输入图像描述

注意:目标ios:11.4,xcode是9.4.1,objective-c

==========================================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;
}

标签: iosobjective-cuitableviewnstableviewheader

解决方案


代替

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

    return vw;
} 

写这个:

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

    return vw;
} 

dequeuResuableCellWithIdentifier 根据文档返回 UITableViewCell :

@available(iOS 6.0, *)

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


推荐阅读