How to set UITableView height accroding to its content height?
I have a question about UITableView
.
I want to let the tableView height according to my cells content height.
So, I use the following code successfully.
DispatchQueue.main.async {
var frame = self.tableView.frame
frame.size.height = self.tableView.contentSize.height
self.tableView.frame = frame
}
But when I have much data to show, my contents will out of screen.
And the tableView also out of screen.
Have any ideas to set it's constrain and don't make it out of screen.
I want to set 15 between the tableView bottom layout and superView bottom layout.
I use SnapKit to set autolayout.
//I want to set this one is the biggest frame size. Other contents I can scroll the tableView to show the data.
tableView.snp.makeConstraints { (make) in
make.top.equalTo(self.topLayoutGuide.snp.bottom)
make.left.equalTo(10)
make.right.equalTo(-10)
make.bottom.equalTo(-15)
}
ios swift uitableview
add a comment |
I have a question about UITableView
.
I want to let the tableView height according to my cells content height.
So, I use the following code successfully.
DispatchQueue.main.async {
var frame = self.tableView.frame
frame.size.height = self.tableView.contentSize.height
self.tableView.frame = frame
}
But when I have much data to show, my contents will out of screen.
And the tableView also out of screen.
Have any ideas to set it's constrain and don't make it out of screen.
I want to set 15 between the tableView bottom layout and superView bottom layout.
I use SnapKit to set autolayout.
//I want to set this one is the biggest frame size. Other contents I can scroll the tableView to show the data.
tableView.snp.makeConstraints { (make) in
make.top.equalTo(self.topLayoutGuide.snp.bottom)
make.left.equalTo(10)
make.right.equalTo(-10)
make.bottom.equalTo(-15)
}
ios swift uitableview
I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?
– dahiya_boy
Nov 23 '18 at 7:37
Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.
– JimmyLee
Nov 23 '18 at 7:40
tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50
– JimmyLee
Nov 23 '18 at 7:41
Can you show me your full code so that I can clearly know what is your requirement?
– Ashvini
Nov 23 '18 at 9:48
I don't undestand what you want here?
– Ashvini
Nov 23 '18 at 9:49
add a comment |
I have a question about UITableView
.
I want to let the tableView height according to my cells content height.
So, I use the following code successfully.
DispatchQueue.main.async {
var frame = self.tableView.frame
frame.size.height = self.tableView.contentSize.height
self.tableView.frame = frame
}
But when I have much data to show, my contents will out of screen.
And the tableView also out of screen.
Have any ideas to set it's constrain and don't make it out of screen.
I want to set 15 between the tableView bottom layout and superView bottom layout.
I use SnapKit to set autolayout.
//I want to set this one is the biggest frame size. Other contents I can scroll the tableView to show the data.
tableView.snp.makeConstraints { (make) in
make.top.equalTo(self.topLayoutGuide.snp.bottom)
make.left.equalTo(10)
make.right.equalTo(-10)
make.bottom.equalTo(-15)
}
ios swift uitableview
I have a question about UITableView
.
I want to let the tableView height according to my cells content height.
So, I use the following code successfully.
DispatchQueue.main.async {
var frame = self.tableView.frame
frame.size.height = self.tableView.contentSize.height
self.tableView.frame = frame
}
But when I have much data to show, my contents will out of screen.
And the tableView also out of screen.
Have any ideas to set it's constrain and don't make it out of screen.
I want to set 15 between the tableView bottom layout and superView bottom layout.
I use SnapKit to set autolayout.
//I want to set this one is the biggest frame size. Other contents I can scroll the tableView to show the data.
tableView.snp.makeConstraints { (make) in
make.top.equalTo(self.topLayoutGuide.snp.bottom)
make.left.equalTo(10)
make.right.equalTo(-10)
make.bottom.equalTo(-15)
}
ios swift uitableview
ios swift uitableview
edited Nov 23 '18 at 7:42
Damon
520318
520318
asked Nov 23 '18 at 7:33
JimmyLeeJimmyLee
12411
12411
I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?
– dahiya_boy
Nov 23 '18 at 7:37
Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.
– JimmyLee
Nov 23 '18 at 7:40
tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50
– JimmyLee
Nov 23 '18 at 7:41
Can you show me your full code so that I can clearly know what is your requirement?
– Ashvini
Nov 23 '18 at 9:48
I don't undestand what you want here?
– Ashvini
Nov 23 '18 at 9:49
add a comment |
I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?
– dahiya_boy
Nov 23 '18 at 7:37
Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.
– JimmyLee
Nov 23 '18 at 7:40
tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50
– JimmyLee
Nov 23 '18 at 7:41
Can you show me your full code so that I can clearly know what is your requirement?
– Ashvini
Nov 23 '18 at 9:48
I don't undestand what you want here?
– Ashvini
Nov 23 '18 at 9:49
I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?
– dahiya_boy
Nov 23 '18 at 7:37
I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?
– dahiya_boy
Nov 23 '18 at 7:37
Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.
– JimmyLee
Nov 23 '18 at 7:40
Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.
– JimmyLee
Nov 23 '18 at 7:40
tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50
– JimmyLee
Nov 23 '18 at 7:41
tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50
– JimmyLee
Nov 23 '18 at 7:41
Can you show me your full code so that I can clearly know what is your requirement?
– Ashvini
Nov 23 '18 at 9:48
Can you show me your full code so that I can clearly know what is your requirement?
– Ashvini
Nov 23 '18 at 9:48
I don't undestand what you want here?
– Ashvini
Nov 23 '18 at 9:49
I don't undestand what you want here?
– Ashvini
Nov 23 '18 at 9:49
add a comment |
6 Answers
6
active
oldest
votes
Have you tried
self.tableView.invalidateIntrinsicContentSize()
https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize
sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.
– JimmyLee
Nov 23 '18 at 7:52
add a comment |
Maybe you can limit the height of the table's frame, making sure is not longer than its superView, something like this modifying your code:
DispatchQueue.main.async {
if let superViewHeight = self.tableView.superView?.bounds.maxY {
let maxHeight = superViewHeight - self.tableView.frame.minY
var frame = self.tableView.frame
frame.size.height = min(self.tableView.contentSize.height, maxHeight)
self.tableView.frame = frame
}
}
add a comment |
Code Work :
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblHeightConstraint: NSLayoutConstraint! // tableView Height Constraint
@IBOutlet weak var tblView: UITableView!
var tblMaxHeight : CGFloat = 50
override func viewDidLoad() {
super.viewDidLoad()
let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height
tblMaxHeight = self.view.frame.size.height - 40 - navHeight
}
override func viewDidLayoutSubviews(){
tblHeightConstraint.constant = min(tblMaxHeight, tblView.contentSize.height)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 24
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = "Row: #(indexPath.row)"
return cell!
}
}
Constraints to tableView :
Output :
add a comment |
Put your UITableView inside a UIScrollView and add constraints like this:
Constraints to Table View
Create an IBOutlet of the height constraint and then override viewWillLayoutSubviews in your UIViewController
override func viewWillLayoutSubviews() {
super.updateViewConstraints()
self.tableViewHeightConstraint.constant = tableView.contentSize.height
}
add a comment |
I have solved a similar problem using the following method. You only need few lines of code.
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
private func setupTableView() {
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}
Simply set an estimated row height for the UITableView and then set the rowHeight as UITableViewAutomaticDimension.
add a comment |
You could create a custom UITableView
:
class AutomaticHeightTableView: UITableView {
override var contentSize: CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height + 20)
}
}
And then set your UITableView
Class to AutomaticHeightTableView
.
This solution is inspired from an answer found on stackoverflow.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442384%2fhow-to-set-uitableview-height-accroding-to-its-content-height%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Have you tried
self.tableView.invalidateIntrinsicContentSize()
https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize
sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.
– JimmyLee
Nov 23 '18 at 7:52
add a comment |
Have you tried
self.tableView.invalidateIntrinsicContentSize()
https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize
sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.
– JimmyLee
Nov 23 '18 at 7:52
add a comment |
Have you tried
self.tableView.invalidateIntrinsicContentSize()
https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize
Have you tried
self.tableView.invalidateIntrinsicContentSize()
https://developer.apple.com/documentation/uikit/uiview/1622457-invalidateintrinsiccontentsize
answered Nov 23 '18 at 7:44
DamonDamon
520318
520318
sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.
– JimmyLee
Nov 23 '18 at 7:52
add a comment |
sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.
– JimmyLee
Nov 23 '18 at 7:52
sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.
– JimmyLee
Nov 23 '18 at 7:52
sorry, add this code in where? I also add this code below the "tableView.rowHeight = UITableViewAutomaticDimension" and not working.
– JimmyLee
Nov 23 '18 at 7:52
add a comment |
Maybe you can limit the height of the table's frame, making sure is not longer than its superView, something like this modifying your code:
DispatchQueue.main.async {
if let superViewHeight = self.tableView.superView?.bounds.maxY {
let maxHeight = superViewHeight - self.tableView.frame.minY
var frame = self.tableView.frame
frame.size.height = min(self.tableView.contentSize.height, maxHeight)
self.tableView.frame = frame
}
}
add a comment |
Maybe you can limit the height of the table's frame, making sure is not longer than its superView, something like this modifying your code:
DispatchQueue.main.async {
if let superViewHeight = self.tableView.superView?.bounds.maxY {
let maxHeight = superViewHeight - self.tableView.frame.minY
var frame = self.tableView.frame
frame.size.height = min(self.tableView.contentSize.height, maxHeight)
self.tableView.frame = frame
}
}
add a comment |
Maybe you can limit the height of the table's frame, making sure is not longer than its superView, something like this modifying your code:
DispatchQueue.main.async {
if let superViewHeight = self.tableView.superView?.bounds.maxY {
let maxHeight = superViewHeight - self.tableView.frame.minY
var frame = self.tableView.frame
frame.size.height = min(self.tableView.contentSize.height, maxHeight)
self.tableView.frame = frame
}
}
Maybe you can limit the height of the table's frame, making sure is not longer than its superView, something like this modifying your code:
DispatchQueue.main.async {
if let superViewHeight = self.tableView.superView?.bounds.maxY {
let maxHeight = superViewHeight - self.tableView.frame.minY
var frame = self.tableView.frame
frame.size.height = min(self.tableView.contentSize.height, maxHeight)
self.tableView.frame = frame
}
}
answered Nov 23 '18 at 7:54
vauxhallvauxhall
725611
725611
add a comment |
add a comment |
Code Work :
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblHeightConstraint: NSLayoutConstraint! // tableView Height Constraint
@IBOutlet weak var tblView: UITableView!
var tblMaxHeight : CGFloat = 50
override func viewDidLoad() {
super.viewDidLoad()
let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height
tblMaxHeight = self.view.frame.size.height - 40 - navHeight
}
override func viewDidLayoutSubviews(){
tblHeightConstraint.constant = min(tblMaxHeight, tblView.contentSize.height)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 24
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = "Row: #(indexPath.row)"
return cell!
}
}
Constraints to tableView :
Output :
add a comment |
Code Work :
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblHeightConstraint: NSLayoutConstraint! // tableView Height Constraint
@IBOutlet weak var tblView: UITableView!
var tblMaxHeight : CGFloat = 50
override func viewDidLoad() {
super.viewDidLoad()
let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height
tblMaxHeight = self.view.frame.size.height - 40 - navHeight
}
override func viewDidLayoutSubviews(){
tblHeightConstraint.constant = min(tblMaxHeight, tblView.contentSize.height)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 24
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = "Row: #(indexPath.row)"
return cell!
}
}
Constraints to tableView :
Output :
add a comment |
Code Work :
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblHeightConstraint: NSLayoutConstraint! // tableView Height Constraint
@IBOutlet weak var tblView: UITableView!
var tblMaxHeight : CGFloat = 50
override func viewDidLoad() {
super.viewDidLoad()
let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height
tblMaxHeight = self.view.frame.size.height - 40 - navHeight
}
override func viewDidLayoutSubviews(){
tblHeightConstraint.constant = min(tblMaxHeight, tblView.contentSize.height)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 24
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = "Row: #(indexPath.row)"
return cell!
}
}
Constraints to tableView :
Output :
Code Work :
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tblHeightConstraint: NSLayoutConstraint! // tableView Height Constraint
@IBOutlet weak var tblView: UITableView!
var tblMaxHeight : CGFloat = 50
override func viewDidLoad() {
super.viewDidLoad()
let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + UIApplication.shared.statusBarFrame.size.height
tblMaxHeight = self.view.frame.size.height - 40 - navHeight
}
override func viewDidLayoutSubviews(){
tblHeightConstraint.constant = min(tblMaxHeight, tblView.contentSize.height)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 24
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = "Row: #(indexPath.row)"
return cell!
}
}
Constraints to tableView :
Output :
edited Nov 23 '18 at 8:09
answered Nov 23 '18 at 8:03
dahiya_boydahiya_boy
4,97411027
4,97411027
add a comment |
add a comment |
Put your UITableView inside a UIScrollView and add constraints like this:
Constraints to Table View
Create an IBOutlet of the height constraint and then override viewWillLayoutSubviews in your UIViewController
override func viewWillLayoutSubviews() {
super.updateViewConstraints()
self.tableViewHeightConstraint.constant = tableView.contentSize.height
}
add a comment |
Put your UITableView inside a UIScrollView and add constraints like this:
Constraints to Table View
Create an IBOutlet of the height constraint and then override viewWillLayoutSubviews in your UIViewController
override func viewWillLayoutSubviews() {
super.updateViewConstraints()
self.tableViewHeightConstraint.constant = tableView.contentSize.height
}
add a comment |
Put your UITableView inside a UIScrollView and add constraints like this:
Constraints to Table View
Create an IBOutlet of the height constraint and then override viewWillLayoutSubviews in your UIViewController
override func viewWillLayoutSubviews() {
super.updateViewConstraints()
self.tableViewHeightConstraint.constant = tableView.contentSize.height
}
Put your UITableView inside a UIScrollView and add constraints like this:
Constraints to Table View
Create an IBOutlet of the height constraint and then override viewWillLayoutSubviews in your UIViewController
override func viewWillLayoutSubviews() {
super.updateViewConstraints()
self.tableViewHeightConstraint.constant = tableView.contentSize.height
}
answered Nov 23 '18 at 9:05
Daniyal RazaDaniyal Raza
26423
26423
add a comment |
add a comment |
I have solved a similar problem using the following method. You only need few lines of code.
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
private func setupTableView() {
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}
Simply set an estimated row height for the UITableView and then set the rowHeight as UITableViewAutomaticDimension.
add a comment |
I have solved a similar problem using the following method. You only need few lines of code.
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
private func setupTableView() {
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}
Simply set an estimated row height for the UITableView and then set the rowHeight as UITableViewAutomaticDimension.
add a comment |
I have solved a similar problem using the following method. You only need few lines of code.
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
private func setupTableView() {
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}
Simply set an estimated row height for the UITableView and then set the rowHeight as UITableViewAutomaticDimension.
I have solved a similar problem using the following method. You only need few lines of code.
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
private func setupTableView() {
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}
Simply set an estimated row height for the UITableView and then set the rowHeight as UITableViewAutomaticDimension.
answered Nov 23 '18 at 9:48
jmsjms
3378
3378
add a comment |
add a comment |
You could create a custom UITableView
:
class AutomaticHeightTableView: UITableView {
override var contentSize: CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height + 20)
}
}
And then set your UITableView
Class to AutomaticHeightTableView
.
This solution is inspired from an answer found on stackoverflow.
add a comment |
You could create a custom UITableView
:
class AutomaticHeightTableView: UITableView {
override var contentSize: CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height + 20)
}
}
And then set your UITableView
Class to AutomaticHeightTableView
.
This solution is inspired from an answer found on stackoverflow.
add a comment |
You could create a custom UITableView
:
class AutomaticHeightTableView: UITableView {
override var contentSize: CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height + 20)
}
}
And then set your UITableView
Class to AutomaticHeightTableView
.
This solution is inspired from an answer found on stackoverflow.
You could create a custom UITableView
:
class AutomaticHeightTableView: UITableView {
override var contentSize: CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height + 20)
}
}
And then set your UITableView
Class to AutomaticHeightTableView
.
This solution is inspired from an answer found on stackoverflow.
answered Nov 23 '18 at 16:55
Marwen DoukhMarwen Doukh
41649
41649
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442384%2fhow-to-set-uitableview-height-accroding-to-its-content-height%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I had done this but in my case I am reloading a section which makes the glitch in section. So how you getting data? and how you showing? and whther it is changable or not?
– dahiya_boy
Nov 23 '18 at 7:37
Just in ViewDidLoad get data. And use simple dataSource function "numberOfRowsInSection" and "cellForRowAt". I don't use reloadData.
– JimmyLee
Nov 23 '18 at 7:40
tableView.rowHeight = UITableViewAutomaticDimension; tableView.estimatedRowHeight = 50
– JimmyLee
Nov 23 '18 at 7:41
Can you show me your full code so that I can clearly know what is your requirement?
– Ashvini
Nov 23 '18 at 9:48
I don't undestand what you want here?
– Ashvini
Nov 23 '18 at 9:49