How to disable scrolling in UITableView table when

2019-01-16 00:47发布

I have a few (grouped style) tables in my iphone app (only on part of the screen and added with Interface Builder though, not subclassed from UITableViewController) that 80% of the time are small and will fit on the screen. When the table fits on the screen, I'd like to disable scrolling, to make it a bit cleaner. But if the table goes off the screen (when rows are later added to it), I'd like to enable scrolling again (because otherwise you can't see that content.)

Is there a way to do this? I can't seem to figure it out. I do know to do:

tableView.scrollEnabled = NO;

but I'm not sure where, or if I have to calculate the table object size or something to get this to work.


Update: Here's the solution that finally worked for me:

if (table.contentSize.height < table.frame.size.height) {
   table.scrollEnabled = NO;
 }
else {
   table.scrollEnabled = YES;
 }

I run this code after calling reloadData on the table, and it calculates the right sizes and appears to work.

table.frame.size.height is the actual size of the object (you can see this in Interface Builder) displayed on the screen, whereas table.contentSize.height is the heights of: the header, the footer, and the height of every cell added together.

9条回答
霸刀☆藐视天下
2楼-- · 2019-01-16 00:58

In Swift:

tableView.alwaysBounceVertical = false
查看更多
smile是对你的礼貌
3楼-- · 2019-01-16 00:58

You can set enable/disable bounce or scrolling the tableview by selecting/deselecting these in the Scroll View area

Scroll View editing area

查看更多
戒情不戒烟
4楼-- · 2019-01-16 01:03

You can edit this in your storyboard (if you are using one). Under the table view there is a checkbox that says "Scrolling Enabled". Uncheck it and you're done.

查看更多
Fickle 薄情
5楼-- · 2019-01-16 01:03

The default height is 44.0f for a tableview cell I believe. You must be having your datasource in hand in a Array? Then just check if [array count]*44.0f goes beyond the frame bounds and if so set tableview.scrollEnabled = NO, else set it to YES. Do it where you figure the datasource out for that particular tableview.

查看更多
\"骚年 ilove
6楼-- · 2019-01-16 01:08

try this

[yourTableView setBounces:NO];
查看更多
Ridiculous、
7楼-- · 2019-01-16 01:08

So there's are multiple answers and requires a all content at once place so I'm adding this answer:

If you're using AutoLayout, by setting this only should work for you:

  • In code:

tableView.alwaysBounceVertical = false

  • or In Interface Builder:

Just find this option and untick "Bounce Vertically" option.

Here's the reference:

enter image description here

If you're not using AutoLayout:

 func viewDidLayoutSubviews() {
    // Enable scrolling based on content height
    self.tableView.scrollEnabled = tableView.contentSize.height > tableView.frame.size.height
 }
查看更多
登录 后发表回答