在UITableView中删除单元格之前的空格 [英] Remove empty space before cells in UITableView

查看:102
本文介绍了在UITableView中删除单元格之前的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将 UITableView 放在不同的位置,而不是放在视图控制器的顶部。有了这个说,它试图在顶部添加标题来考虑导航栏,但这不是必需的,因为我没有在控制器的顶部。



如果我把 UITableView 的左上角放在我想要的单元格的位置,它就不会将单元格放在那里:





现在,如果我只是向上移动表格视图以便单元格位于正确的位置,我会遇到一个不同的问题 - 滚动时单元格可以移动到那里(这是我的手指在导航控制器上方):





当我放手时,是的 - 单元格将在搜索栏的正下方,但这显然是一个问题,如何将它们带到它上面。



我将如何做到这一点?有没有更简单的方法?

解决方案

执行 UITableView的单元格显示在空白区域?



如果是这样,那么问题可能是添加到的插入内容UITableView 因为您在视图中有导航控制器。
将插入内容添加到表视图中,以便在没有滚动时将内容放在导航栏下方。滚动表格时,内容会滚动并显示在透明导航栏下。当然,只有当表视图直接在导航栏下面开始时才需要这种行为,这不是这里的情况。



需要注意的另一件事是iOS调整内容插入仅适用于视图层次结构中的第一个视图,如果它是 UIScrollView 或它的后代(例如 UITableView UICollectionView )。如果视图层次结构包含多个滚动视图,则 automaticAdjustsScrollViewInsets 将仅对第一个进行调整。



以下是更改此行为的方法:



a)界面生成器




  • 选择视图控制器

  • 打开属性检查器

  • 在IB的属性检查器中(当选择了视图控制器时),有一个名为调整滚动视图插入的属性,默认情况下处于打开状态。取消选中此选项:





    (图片由 Dheeraj D




我不确定哪个Xcode版本引入了这个选项(没有在发行说明中发现它),但它至少在版本5.1.1中可用。



编辑:为避免混淆,这是评论中提到的第三个选项



b)以编程方式



将此添加到ie viewDidLoad (归功于 Slavco Petkovski 的回答和 Cris R 的评论)

  // Objective-C 
self.automaticallyAdjustsScrollViewInsets = NO;

// Swift
self.automaticallyAdjustsScrollViewInsets = false

c)这可能与老学生有关



你可以通过添加



来解决这个问题pre> tableView.contentInset = UIEdgeInsetsZero

// Swift 3更改
tableView.contentInset = UIEdgeInsets.zero

或者如果您使用IB并且导航栏不透明(无法从屏幕截图中看出)




  • 选择视图控制器

  • 打开属性检查器

  • 在视图控制器选项中扩展边缘部分取消选择在顶栏下


I am currently trying to put a UITableView in a different location rather than at the top of my view controller. With this said, it is trying to add the header in the top to account for the navigation bar, but this is not needed since I do not have at the top of my controller.

If I put the top left corner of the UITableView where I want the cells to be, it doesn't put the cells there:

Now, if I just move the table view up so the cells are in the correct place, I get a different problem - the cells can be moved up to there when scrolling (this is with my finger above the navigation controller):

When I let go, yes - the cells will go right below the search bar, but this is obviously a problem how you can bring them above it.

How would I go about doing this? Is there any easier way?

解决方案

Do the cells of the UITableView show on the empty space when you scroll down?

If so, then the problem might be the inset that is added to the UITableView because of the Navigation controller you have in your view. The inset is added to the table view in order for the content to be placed below the navigation bar when no scrolling has occurred. When the table is scrolled, the content scrolls and shows under a transparent navigation bar. This behavior is of course wanted only if the table view starts directly under the navigation bar, which is not the case here.

Another thing to note is that iOS adjusts the content inset only for the first view in the view hierarchy if it is UIScrollView or it's descendant (e.g. UITableView and UICollectionView). If your view hierarchy includes multiple scroll views, automaticallyAdjustsScrollViewInsets will make adjustments only to the first one.

Here's how to change this behavior:

a) Interface Builder

  • Select the view controller
  • Open Attributes inspector
  • There's a property called "Adjust scroll view insets" in IB's attribute inspector (when a view controller is selected) which is on by default. Uncheck this option:


    (Image courtesy of Dheeraj D)

I'm not sure which Xcode version introduced this option (didn't spot it in the release notes), but it's at least available in version 5.1.1.

Edit: To avoid confusion, this was the third option mentioned in the comments

b) Programmatically

Add this to i.e. viewDidLoad (credits to Slavco Petkovski's answer and Cris R's comment)

// Objective-C
self.automaticallyAdjustsScrollViewInsets = NO;

// Swift
self.automaticallyAdjustsScrollViewInsets = false

c) This might be relevant for old schoolers

You can either fix this by adding

tableView.contentInset = UIEdgeInsetsZero

//Swift 3 Change
tableView.contentInset = UIEdgeInsets.zero

Or if you are using IB and if the navigation bar is not transparent (can't tell from the screenshot)

  • Select the view controller
  • Open Attributes inspector
  • In View Controller options Extend Edges section deselect "Under Top Bars"

这篇关于在UITableView中删除单元格之前的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆