【问题标题】:Cannot see buttons on UITableView header在 UITableView 标题上看不到按钮
【发布时间】:2018-07-11 08:09:35
【问题描述】:

我跟随here添加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;
}

【问题讨论】:

  • 您是否添加了自定义标题单元格类?我在这张图片中看不到任何文件名 HeaderCell。
  • 有一个标识符为HeaderCell的单元格。

标签: ios objective-c uitableview nstableviewheader


【解决方案1】:

代替

- (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

【讨论】:

  • 感谢@iPeter,不管有没有变化,返回的vw都是nil。
  • 所以你的heightForHeaderviewForHeader 被调用了?
  • 是的,两个都被调用了,我在里面设置了断点,两个都被命中了。而且我还覆盖了 cellforrow 和 numberofrows
  • 为什么要注释掉注册 HeaderCell 到 tableView 的代码?
【解决方案2】:

我已经浏览了之前的评论。你可以试试关注

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

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

并检查 vw 中的值。

【讨论】:

  • 不行,我怀疑是xcode 9.4.1的bug,试了很多方法,还是不行。
  • 你必须返回 vw.contentView。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多