ios 删除 UITableView 中单元格前的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18906919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:26:02  来源:igfitidea点击:

Remove empty space before cells in UITableView

iosobjective-cuitableviewios7

提问by hetelek

I am currently trying to put a UITableViewin 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.

我目前正在尝试将 aUITableView放在不同的位置,而不是放在我的视图控制器的顶部。话虽如此,它试图在顶部添加标题以说明导航栏,但这不是必需的,因为我的控制器顶部没有。

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

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

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?

我该怎么做呢?有没有更简单的方法?

回答by lekksi

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

UITableView向下滚动时,单元格是否显示在空白处?

If so, then the problem might be the inset that is added to the UITableViewbecause 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.

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

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

另一件需要注意的事情是,iOS 只为视图层次结构中的第一个视图调整内容插入,如果它是UIScrollView或者它的后代(例如UITableViewUICollectionView)。如果您的视图层次结构包含多个滚动视图,automaticallyAdjustsScrollViewInsets则只会对第一个进行调整。

Here's how to change this behavior:

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

a) Interface Builder

a) 界面生成器

  • 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)

  • 选择视图控制器
  • 打开属性检查器
  • 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. 取消选中此选项:


    (图片由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.

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

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

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

b) Programmatically

b) 以编程方式

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

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

// Objective-C
self.automaticallyAdjustsScrollViewInsets = NO;

// Swift
self.automaticallyAdjustsScrollViewInsets = false

c) This might be relevant for old schoolers

c) 这可能与老学生有关

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)

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

  • Select the view controller
  • Open Attributes inspector
  • In View Controller options Extend Edges section deselect "Under Top Bars"
  • 选择视图控制器
  • 打开属性检查器
  • 在“视图控制器”选项“扩展边缘”部分取消选择“在顶部栏下”

回答by irblue

I had this same problem and I'm pretty sure I had a UIView in there at one point. Simply copying the entire table view, deleting it, and pasting it again fixed it for me.

我遇到了同样的问题,我很确定我曾经在那里有一个 UIView。只需复制整个表视图,将其删除,然后再次粘贴即可为我修复。

回答by netshark1000

Select the tableview in your storyboard and ensure that the style is set to "Plain", instead of "Grouped". You can find this setting in the attributes Inspector tab.

在故事板中选择 tableview 并确保将样式设置为“Plain”,而不是“Grouped”。您可以在属性检查器选项卡中找到此设置。

回答by Andre Cytryn

In My Case I had a UILabel under the UITableView in view hierarchy.

在我的案例中,我在视图层次结构中的 UITableView 下有一个 UILabel。

I moved it "forward" and the blank space appeared. Not sure why but it works like this, if theres anything under the tableView, it hides the blank space.

我将它“向前”移动,空白区域出现了。不知道为什么,但它是这样工作的,如果 tableView 下有任何东西,它会隐藏空白空间。

Also you can try checking/uncheking "Adjust Scroll View Insets" on your view controller inspector on storyboard.

您也可以尝试在故事板上的视图控制器检查器上检查/取消选中“调整滚动视图插入”。

enter image description here

在此处输入图片说明

回答by Tung Fam

NONE of the above helped me.but doing this did help:

以上都没有帮助我。但这样做确实有帮助

self.tableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0)

instead of '-64' you can put any other number depending on the height of your navigation bar.

您可以根据导航栏的高度输入任何其他数字,而不是“-64”。

回答by Jacobo Koenig

If you are doing this on iOS 11, automaticallyAdjustsScrollViewInsets won't work as it is deprecated, the following code worked for me as per the answer in this post: https://stackoverflow.com/a/44391269/5476481

如果您在 iOS 11 上执行此操作,则automatedAdjustsScrollViewInsets 将不起作用,因为它已被弃用,以下代码根据本文中的答案对我有用:https: //stackoverflow.com/a/44391269/5476481

        if #available(iOS 11.0, *) {
            tblView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }

Hope this helps.

希望这可以帮助。

回答by Marwen Doukh - Maru?n Duch

In the Storyboard , set the contentInsetsof your tableviewto Never

在故事板,将contentInsets您的tableview从未

enter image description here

在此处输入图片说明

回答by akaDuality

There is may be pre-added UIRefreshControl. I have this problem with code like this:

有可能是预先添加的UIRefreshControl。我有这样的代码问题:

self.refreshControl = [[UIRefreshControl alloc] init];{
    [self.refreshControl setAttributedTitle:[[NSAttributedString alloc]
                                             initWithString:@"Your string"]];
    [self.refreshControl addTarget:self
                            action:@selector(updateScreen)
                  forControlEvents:UIControlEventValueChanged];

}

As you see, I set self.refreshControllerimmediately after init

如你所见,我self.refreshController立即设置init

But, if you set refreshControlafter setup, then top-space don't interfere you.

但是,如果您在设置refreshControl后进行设置,则顶部空间不会干扰您。

UIRefreshControl *refreshControl  = [[UIRefreshControl alloc] init];{
    [self.refreshControl setAttributedTitle:[[NSAttributedString alloc]
                                             initWithString:@"Your String"]];
    [self.refreshControl addTarget:self
                            action:@selector(updateScreen)
                  forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refreshControl;
}

回答by Mike S

I can't say for sure but it looks like you've got an extra UIView stuck between where the Prototype Cells begin and the top of the UITableView. My image looks exactly like yours if you remove the text/lines I added in.

我不能肯定地说,但看起来你有一个额外的 UIView 卡在原型单元格的开始和 UITableView 的顶部之间。如果您删除我添加的文本/行,我的图像看起来与您的完全一样。

enter image description here

在此处输入图片说明

回答by hetelek

I'm still not sure what caused that extra space at the top, but I found that deleting the UITableView with the space and replacing it with a new one took the space away.

我仍然不确定是什么导致了顶部的额外空间,但我发现删除带有空间的 UITableView 并用新的替换它会占用空间。

I must have dragged a UIView in there accidentally, but I couldn't select it so I couldn't delete.

我一定是不小心在那里拖了一个 UIView,但我无法选择它,所以我无法删除。