How to disable scrolling in UITableView table when the content fits on the screen

后端 未结 9 2400
孤街浪徒
孤街浪徒 2020-12-07 07:36

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 UITableViewC

相关标签:
9条回答
  • 2020-12-07 07:44

    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.

    0 讨论(0)
  • 2020-12-07 07:52

    I think you want to set

    tableView.alwaysBounceVertical = NO;
    
    0 讨论(0)
  • 2020-12-07 07:53

    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:

    If you're not using AutoLayout:

     override func viewDidLayoutSubviews() {
        // Enable scrolling based on content height
        tableView.isScrollEnabled = tableView.contentSize.height > tableView.frame.size.height
     }
    
    0 讨论(0)
  • 2020-12-07 07:54

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

    Scroll View editing area

    0 讨论(0)
  • 2020-12-07 07:56

    You can verify the number of visible cells using this function:

    - (NSArray *)visibleCells
    

    This method will return an array with the cells that are visible, so you can count the number of objects in this array and compare with the number of objects in your table.. if it's equal.. you can disable the scrolling using:

    tableView.scrollEnabled = NO;
    

    As @Ginny mentioned.. we would can have problems with partially visible cells, so this solution works better in this case:

    tableView.scrollEnabled = (tableView.contentSize.height <= CGRectGetHeight(tableView.frame));
    

    In case you are using autoLayout this solution do the job:

    tableView.alwaysBounceVertical = NO.
    
    0 讨论(0)
  • 2020-12-07 08:00

    In Swift:

    tableView.alwaysBounceVertical = false
    
    0 讨论(0)
提交回复
热议问题