Adjust the size of UITableView dynamically which would display as a non-scrollable list of content
You can change height of the tableview based on the content.
var frame = tableView.frame
frame.size.height = tableView.contentSize.height
tableView.frame = frame
If you are using autolayout create outlet for tableview height and assign tableview content size to its constant.
tableviewHeightConstraint.constant = tableview.contentSize.height
adjusting tableView height while using self sizing cells and a non scrollable table
You can do it from the following steps ->
- Construct the tableview within the UIView with 0 Leading, Top, bottom, Trailing constant
- Add height constraint of the UIView and define outlet of it
3. Load Data in the tableview and get tableview's ContentHeight
- Update the height constraint of the UIView equal to the tableview's ContentHeight
Dont forget to call layoutIfNeeded() after updating constraint
class ViewController: UIViewController {
@IBOutlet weak var containerViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var nameTableView: UITableView!
var dataArrary = [String]()
override func viewDidLoad() {
super.viewDidLoad()
nameTableView.rowHeight = UITableViewAutomaticDimension
nameTableView.estimatedRowHeight = 44.0
nameTableView.tableFooterView = UIView(frame:CGRectZero)
nameTableView.tableHeaderView = UIView(frame:CGRectZero)
dataArrary = ["item1 bshfklsdflkjsdfkljsdklfjlsdkfjlksdjflksdjflkdsjflkdsjflksdjflkjdslkfjdslkjflkdsfjdsfkljsdflkjdslkfj", "item2hfdshjgfhjdsgfhjdgfhjdgfhjdgfhjgfhsdf", "item3", "item4", "item5", "item6kfjdskfljsdlkfjlksdfjlksdjfkldsjfklfjdkslf"]
}
override func viewDidAppear(animated: Bool) {
updateView()
}
func updateView() {
self.nameTableView.scrollEnabled = false
self.nameTableView.frame.size = CGSizeMake(self.view.frame.width, self.nameTableView.contentSize.height)
containerViewHeightConstraint.constant = self.nameTableView.frame.height
// assign height equal to the tableView's Frame
self.view.layoutIfNeeded()
// redraws views and subviews with updated constraint
}
}
extension ViewController:UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArrary.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.numberOfLines = 0
cell.textLabel?.text = dataArrary[indexPath.row]
return cell
}
}
- It should look like this
UITableView dynamic cell heights only correct after some scrolling
I don't know this is clearly documented or not, but adding [cell layoutIfNeeded]
before returning cell solves your problem.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TestCell"];
NSUInteger n1 = firstLabelWordCount[indexPath.row];
NSUInteger n2 = secondLabelWordCount[indexPath.row];
[cell setNumberOfWordsForFirstLabel:n1 secondLabel:n2];
[cell layoutIfNeeded]; // <- added
return cell;
}
Swift - Non-scrollable UITableViewController inside UITableViewController
For those who are looking at this in the future, what I ended up doing was separating the posts into tableView sections, rather than cells, and then adding one cell for the profileTableViewController, and cells for the comments underneath it.
Related Topics
Uiscrollview's Origin Changes After Popping Back to the Uiviewcontroller
What's Dead & Exploded in Swift's Exception Stack
How to Get the Custom Overlay for Uiimagepicker Camera to Be Full Screen in iOS 7
Creating a 3X3 Grid with Auto Layout Constraints
iOS Extensions with Multiple Targets
Status Bar Showing Black Text, Only on iPhone 6 iOS 8 Simulator
Uilabel Text Not Being Updated
How to Open Location Services Screen from Setting Screen
iOS 9 - "Attempt to Delete and Reload the Same Index Path"
iOS - Uiimagewritetosavedphotosalbum
Nsuserdefaults Unreliable in iOS 8
How to Loop Over Struct Properties in Swift
No Round Rect Button in Xcode 5
iOS App, Programmatically Get Build Version
Xcode 8 Recommend Me to Change the Min iOS Deployment Target from 7.1 to 8.0
Differences Between Websockets and Long Polling for Turn Based Game Server