0

I have a UITableView inside of a UIView that I perform the function insertRowsAtIndexPaths on. More content appears, but the UITableView height remains the same. I've read through hundreds of SO posts for days now, and can't seem to understand why this isn't updating.

var tableView       = UITableView()
tableView.frame      = CGRectMake(0, 0, self.view.frame.width, view.frame.height)
tableView.estimatedRowHeight = 40
tableView.scrollEnabled = true
tableView.userInteractionEnabled  = true
tableView.rowHeight = UITableViewAutomaticDimension
tableView.delegate   = self
tableView.dataSource = self

Then I if I click a cell..

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let (parent, isParentCell, actualPosition) = self.findParent(indexPath.row)

    self.tableView.beginUpdates()
    self.updateCells(parent, index: indexPath.row)
    self.tableView.endUpdates()

    var frame : CGRect = self.tableView.frame
    print(frame.size) // Returns -> (375.0, 667.0)
    frame.size = self.tableView.contentSize
    print(frame.size) // Return -> (375.0, 1151.0)
    self.tableView.frame = frame

}

But the height of the UITableView remains 667.0 as seen here :

enter image description here

.. even though you can clearly see the contentSize now transcends the bounds.

What could I possibly be missing here?


Update

-- Here is how I draw cellForRowAtIndexPath ..

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell : UITableViewCell!

    let (parent, isParentCell, actualPosition) = self.findParent(indexPath.row)

    if !isParentCell {
        cell = tableView.dequeueReusableCellWithIdentifier(childCellIdentifier, forIndexPath: indexPath)
        cell.textLabel!.text        = self.dataSource[parent].childs[indexPath.row - actualPosition - 1]
        cell.textLabel!.textColor   = UIColor(red: 35/255.0, green: 31/255.0, blue: 32/255.0, alpha: 1.0)
        cell.textLabel!.font        = UIFont(name: "STHeitiTC-Light", size: 16)
        cell.textLabel!.layer.borderColor = UIColor.purpleColor().CGColor
        cell.textLabel!.layer.borderWidth = 1.0
    }
    else {
        cell = tableView.dequeueReusableCellWithIdentifier(parentCellIdentifier, forIndexPath: indexPath)
        cell.textLabel!.text        = self.dataSource[parent].title
        cell.textLabel!.textColor   = UIColor(red: 66/255.0, green: 116/255.0, blue: 185/255.0, alpha: 1.0)
        cell.textLabel!.font        = UIFont(name: "STHeitiTC-Light", size: 20)
    }
    cell.textLabel!.frame = CGRectMake(0,0,self.view.frame.width, CGFloat.max)
    cell.selectionStyle                                       = .None
    cell.textLabel!.translatesAutoresizingMaskIntoConstraints = false
    cell.textLabel!.numberOfLines                             = 0
    cell.textLabel!.sizeToFit()
    cell.textLabel!


    return cell
}

A visual of the problem..

enter image description here

Trip
  • 26,756
  • 46
  • 158
  • 277
  • When you add new rows to the tableview the contentSize will increase, the frame of tableview will be same as tableview will scroll after content size increases – HardikDG Mar 26 '16 at 09:17
  • @Pyro ah interesting. sounds like something else is preventing my tableView from being scrollable then..Would there be anything else I can show you in my code that you would imagine would prevent me from scrolling down to see the new content? – Trip Mar 26 '16 at 09:54

2 Answers2

1

The frame of the table view is the actual size that it takes in its superview. The content size of the tableview is the size of its content. Since the tableview is scrollable this two values are not related. The frame size is the size of the window through which you see the content of the tableview. The content can be outside of that window and then you cannot see it but you can scroll to see the content you want.

Jelly
  • 4,522
  • 6
  • 26
  • 42
  • Interesting. So then, I guess, what I'm noticing is that my contentView is indeed expanding, but it's not allowing me to scroll its entire new span. Any ideas as to why this might be? Or what I might be able to investigate ( specific constraints or values, etc. ) to find more information on what might be preventing this movement? – Trip Mar 26 '16 at 09:56
  • This should not happen only if you explicitly specify something like that, which I think you do not. Could you explain what actually happens? You can't see the whole content after you insert some new one? Can you see it before? Do you have more content than the frame height (so can you scroll) initally? – Jelly Mar 26 '16 at 10:08
  • I added a gif of the problem at the bottom of my question for you. I'm clicking on a UITableView Cell that onTouch, performs `insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Fade)` . Once this new content is added, I can't seem to scroll to the entire distance – Trip Mar 26 '16 at 10:13
  • 1
    The problem is that the table view takes into account `tableView.estimatedRowHeight = 40` but the height of the cell you insert is much greater. Try using better approximation something like 200, 400 from what I see in the GIF you posted – Jelly Mar 26 '16 at 10:20
  • hmm.. tried experimenting with the approximation.. 200, 400 - no changes. – Trip Mar 26 '16 at 10:34
  • Check this thread: http://stackoverflow.com/questions/25971147/uitableview-dynamic-cell-heights-only-correct-after-some-scrolling – Jelly Mar 26 '16 at 11:01
1

When you add new rows to the tableview the contentSize will increase, the frame of tableview will be same as tableview will scroll after content size increases

For programatically it will be like

tableView.userInteractionEnabled  = true
tableView.scrollEnabled = true

Else you can check the constraint if you have applied auto layout - it should be like marigin from top,bottom,left,right or you can specifiy height/width also instead of bottom/right margin


Update: : Auto Layout Looks like Autolayout is the issue for UITableViewAutomaticDimension, according to apple doc, one tutorial and one answer in the SO this AutomaticDimension only work when you have auto layout enabled, so because of that it may not calculating the correct height

Apple Doc:Self sizing

tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableViewAutomaticDimension

As soon as both of these properties are set, the system uses Auto Layout to calculate the row’s actual height.

For the demo of Self sizing with the Auto layout you can check the following link :

Self sizing cell demo:Appcoda

From Appcoda

However, without auto layout, self sizing cells won’t work as it relies on the constraints to determine the proper row height.

Auto layout can be done both from Interface Builder and Programatically: Interface Build is shown in the appcoda demo for the basic of programatically AutoLayout
Apple Doc: Programatically Auto Layout
Auto Layout tutorial

HardikDG
  • 5,892
  • 2
  • 26
  • 55
  • I am adding my tableView's programmatically. And I just added those two lines like you mentioned -- but it still doesn't allow me to scroll down. -- To your question, yes I embed the UITableView inside a UIView which is technically my main ViewController's view. – Trip Mar 26 '16 at 10:09
  • Yes, it is. As per the print() commands I was running in the question. You can see that they increase there. – Trip Mar 26 '16 at 10:15
  • how you are displaying the content in the tableview cell ? in label,scrollview,webview like this ?, also please try to slide from the right corner of the phone – HardikDG Mar 26 '16 at 10:17
  • I'm going to guess that the issue is that the UILabel height in my `cell` does not change. For the longest time, my text was being cut off by the UILabel where the frame of UILabel would be have a set width, and the text would end with `...`. I fixed this by setting `tableView.estimatedRowHeight = 40`, the text all started appearing. But it was a false discovery, because I think it still thinks that the cell is that high. I'll update myanswer to show my `cellForRowAtIndexPath` content – Trip Mar 26 '16 at 12:19
  • i have update the answer with autolayout, please check that – HardikDG Mar 26 '16 at 12:49
  • thanks so much for helping me. I ended up getting the answer by using every trick in the book and a lot of it came from you. – Trip Mar 26 '16 at 13:35