ios - 在 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;
}
解决方案
代替
- (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
推荐阅读
- docker - 如何从 docker 容器内运行的服务中查找客户端源 IP 地址
- docker - Docker Swarm - 是否有只读模式?
- arrays - Perl:以分割线元素为键读取哈希
- java - 查找内部列表列表的平均值
- > 使用 Stream 并以 List 形式返回结果
由内部列表的平均值组成 - electron - 使用 Electron Builder 对 DLL 文件进行代码签名
- amazon-web-services - 在 aws CodeBuild 中获取提交 SHA
- icalendar - ICS 方法 CANCEL 导致多个日历应用程序出错
- javascript - 无法使用 puppeteer 修改元素
- linux - 从 linux 编码解码阿拉伯字符
- javascript - Leaflet.js setView() / fitBounds() 不尊重填充