Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable tableView scrolling?

Tags:

ios

swift

When I add a tableView in a viewController by default the tableView scrolls, but I want it static. Is it possible?

Here is my code:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
   return names.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableview.dequeueReusableCellWithIdentifier("cell") as! TableViewCell
    cell.username.text = names[indexPath.row]
    return cell
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
    return 1
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return 50.0
}
like image 356
Bansal Avatar asked Sep 09 '16 07:09

Bansal


People also ask

How does a table view scroll back to a specific row?

When the user scrolls back to a row, the table view will request its data again. The tableView(_:numberOfRowsInSection:) method tells the table view how many elements a section contains. The table view uses this information to prepare its scrolling area and display a scroll bar of the appropriate size.

Should I use a scroll view or a UITableView?

For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views. The code of the table view data source often ends inside view controllers when it should go into a separate class.

Why use a UITableView instead of a Tableview?

Table views are more versatile than you might think. For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views.

Should I use auto layout for scroll views?

In a scroll view, you need to figure the frame of each subview by yourself. You can use Auto Layout, but that still means you will need a lot of code to set all the constraints. In a scroll view, that’s not as easy to get right as it is in a regular UIView.


2 Answers

Swift 3, 4 and 5:

tableView.isScrollEnabled = false
like image 183
Igor Biscanin Avatar answered Sep 30 '22 13:09

Igor Biscanin


Disable Scrolling in Interface Builder. Select the table view and the Attributes Inspector ⌥⌘4

Then uncheck Scrolling Enabled

enter image description here

like image 33
vadian Avatar answered Sep 30 '22 14:09

vadian