Cod*_*odo 102 uitableview ios ios7
由于使用分组样式的表视图设计在iOS 7中发生了很大变化,我想隐藏(或删除)第一个节头.到目前为止,我还没有成功实现它.
有点简化,我的代码看起来像这样:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return 0.0f;
return 32.0f;
}
- (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0) {
UIView* view = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 640.0f, 0.0f)];
return view;
}
return nil;
}
- (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return nil;
} else {
// return some string here ...
}
}
Run Code Online (Sandbox Code Playgroud)
如果我返回0的高度,则永远不会使用截面索引0调用其他两个方法.但仍然使用默认高度绘制空截面标题.(在iOS 6中,调用了两种方法.但是,可见结果是相同的.)
如果我返回不同的值,则节标题将获得指定的高度.
如果我返回0.01,那几乎是正确的.但是,当我在模拟器中打开"颜色未对齐图像"时,它会标记所有表格视图单元格(这似乎是一个逻辑结果).
问题UITableView:从空部分隐藏标题的答案似乎表明有些人成功隐藏了部分标题.但它可能适用于普通样式(而不是分组样式).
到目前为止,最好的折衷方案是返回高度0.5,导致导航栏下方的线条稍微粗一些.但是,如果有人知道如何完全隐藏第一部分标题,我将不胜感激.
更新
根据caglar的分析(/sf/answers/1333977641/),只有在表视图包含在导航控制器中时才会出现问题.
Cod*_*odo 143
我有一个似乎相当干净的解决方法.所以我在回答我自己的问题.
由于0作为第一节标题的高度不起作用,我返回1.然后我使用contentInset隐藏导航栏下方的高度.
Objective-C的:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return 1.0f;
return 32.0f;
}
- (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return nil;
} else {
// return some string here ...
}
}
- (void) viewDidLoad
{
[super viewDidLoad];
self.tableView.contentInset = UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0);
}
Run Code Online (Sandbox Code Playgroud)
迅速:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return section == 0 ? 1.0 : 32
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.contentInset = UIEdgeInsets(top: -1, left: 0, bottom: 0, right: 0)
}
Run Code Online (Sandbox Code Playgroud)
use*_*170 48
这是如何隐藏UITableView中的第一个节头(分组样式).
Swift 3.0和Xcode 8.0解决方案
TableView的委托应该实现heightForHeaderInSection方法
在heightForHeaderInSection方法中,返回最小正数.(不是零!)
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
let headerHeight: CGFloat
switch section {
case 0:
// hide the header
headerHeight = CGFloat.leastNonzeroMagnitude
default:
headerHeight = 21
}
return headerHeight
}
Run Code Online (Sandbox Code Playgroud)Gio*_*Ath 36
对我和我的团队来说,答案非常有趣,并且像魅力一样工作
原因:
我们观察到,如果第一个视图是UITableView,则仅在视图层次结构中的第一个视图中发生这种情况.所以,除了第一个,所有其他类似的UITableViews都没有这个烦人的部分.我们尝试将UITableView移出视图层次结构中的第一个位置,一切都按预期工作.
Nit*_*rad 23
将此技巧用于分组类型tableView
在viewDidLoad方法中为表视图的代码复制粘贴:
tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, 0.01f)];
Run Code Online (Sandbox Code Playgroud)
小智 19
这样就行了.
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return CGFloat.min
}
return 25
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
return nil
}else {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我刚刚复制了你的代码并试过了.它运行正常(在模拟器中试用).我附上了结果视图.你想要这样的观点吧?或者我误解了你的问题?

Swift3:heightForHeaderInSection与0一起使用,你只需要确保标题设置为clipsToBounds.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
}
Run Code Online (Sandbox Code Playgroud)
如果你没有设置clipsToBounds隐藏标题将在滚动时可见.
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let header = view as? UITableViewHeaderFooterView else { return }
header.clipsToBounds = true
}
Run Code Online (Sandbox Code Playgroud)
更新[9/19/17]:在iOS 11中,旧答案不再对我有用。感谢Apple。做到了以下几点:
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 20.0f;
self.tableView.contentInset = UIEdgeInsetsMake(-18.0, 0.0f, 0.0f, 0.0);
Run Code Online (Sandbox Code Playgroud)
上一个答案:
正如克里斯·奥斯托莫(Chris Ostomo)的评论中所述,以下内容对我有用:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return CGFLOAT_MIN; // to get rid of empty section header
}
Run Code Online (Sandbox Code Playgroud)
以下是如何在 Swift 中删除分组 UITableView 中的顶部部分标题:
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
103216 次 |
| 最近记录: |