Make Uitableview Not Scrollable and Adjust Height to Accommodate All Cells

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 ->

  1. Construct the tableview within the UIView with 0 Leading, Top, bottom, Trailing constant
  2. Add height constraint of the UIView and define outlet of it

Sample Image
3. Load Data in the tableview and get tableview's ContentHeight


  1. 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
}

}

  1. It should look like this

Sample Image

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



Leave a reply



Submit