从iOS7开始,在我的UITableView顶部有额外的空间它有一个UITableViewStyleGrouped样式。
这里有一个例子:
tableview从第一个箭头开始,有35个像素的无法解释的填充,然后绿色的头是一个由viewForHeaderInSection返回的UIView(其中section为0)。
有人能解释一下这个35像素的数量是从哪里来的吗?我如何才能在不切换到UITableViewStylePlain的情况下摆脱它?
更新(回答):
在iOS 11及更高版本中:
tableView.contentInsetAdjustmentBehavior = .never
感谢@Aurelien Porte的回答。这是我的解决方案
产生此问题的原因:-
UITableView不喜欢头的高度为0.0。如果你要做的是有一个高度为0的标题,你可以跳到解决方案。
即使以后你给你的头分配了一个非0.0的高度,UITableView也不喜欢一开始就被分配一个高度为0.0的头。
在ViewDidLoad: -
self.edgesForExtendedLayout = UIRectEdge.None
self.automaticallyAdjustsScrollViewInsets = false
不需要这样的东西:-
self.myTableview.contentInset = UIEdgeInsetsMake(-56, 0, 0, 0)
在highforheaderinsection委托中:-
if section == 0
{
return 1
}
else
{
return 40; // your other headers height value
}
在viewForHeaderInSection委托中:-
if section == 0
{
// Note CGFloat.min for swift
// For Objective-c CGFLOAT_MIN
let headerView = UIView.init(frame: CGRectMake(0.0, 0.0, self.myShaadiTableview.bounds.size.width, CGFloat.min))
return headerView
}
else
{
// Construct your other headers here
}
下面的操作(Swift)解决了这个问题,但当你不需要头文件的时候,这是有效的。
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.min
}
如果你这样做,你将不得不放弃第一部分,并使用其他内容。
UITableViewDataSource实施:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return <number_of_data_sections>+1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// the first section we don't use for data
if section == 0 {
return 0
}
// starting from 1, there are sections we use
if section == 1 {
let dataSection = section - 1
// use dataSection for your content (useful, when data provided by fetched result controller). For example:
if let sectionInfo = myFRC!.sections![dataSection] as? NSFetchedResultsSectionInfo {
return sectionInfo.numberOfObjects
}
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let dataIndexPath = NSIndexPath(forRow: indexPath.row, inSection: (indexPath.section - 1) )
// return cell using transformed dataIndexPath
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 1 {
// return your header height
}
return CGFloat.min
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 1 {
// return your header view
}
return nil
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
// in my case, even when 1st section header was of zero heigh, I saw the space, an that was a footer. I did not need footer at all, so always gave zero height
return CGFloat.min
}
就是这样。模型不知道任何变化,因为我们在访问数据时转换了节号。
你可以检测你的应用是否运行iOS7或更高版本,并在你的表视图委托中添加这两个方法(通常在你的UIViewController代码中)
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
这可能不是一个优雅的解决方案,但对我来说是可行的
斯威夫特版本:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
还有另一种方式……但我喜欢它,因为它避免了硬编码任何高度值。
在UITableViewStyleGrouped表(静态或动态)中,只需在tableView中分配所需的高度(_:highightforheaderinsection)。我计算的新高度基于底部填充为节头标签。
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
var height = UITableViewAutomaticDimension
if section == 0, let header = tableView.headerViewForSection(section) {
if let label = header.textLabel {
// get padding below label
let bottomPadding = header.frame.height - label.frame.origin.y - label.frame.height
// use it as top padding
height = label.frame.height + (2 * bottomPadding)
}
}
return height
}
在iOS 9和Xcode 7.3上测试。
希望能有所帮助。