How to update UITableViewCell height because of text entered into UITextView
Asked Answered
C

3

9

I have different examples where we can update the UITableViewCell height based on growing UITextView which actually is working for me. The issue which I am facing is, I have more subviews below UITextView inside a UITableViewCell. This way, the cell's height updates but the position of the subviews remain fixed which causes overlap of UITextView and the subviews. Just to mention, I am not using auto layout.
How do I fix this ?
These are the three screenshots which will help in understanding my issue :

1. Before TextView is shown :

enter image description here

2. After TextView is shown :

enter image description here

3. After text is entered :

enter image description here

Candra answered 9/12, 2015 at 18:26 Comment(2)
In your tableview cell put all your subview(that remain fixed) in one view and when your UITextview height change(increase/decrease) set that view y position below UITextview.Leningrad
What language are you using?Aisne
K
3

I'm assuming you are using auto layout for this cell (but we could use a code example or Xcode screenshot to help you more specifically). If you are using auto layout, you'll want to make sure that:

  1. You have a constraint between the UITextView and the other UIView subviews below it in the cell
  2. The other UIView subviews are eventually constrained to the bottom of the cell.
Knockknee answered 9/12, 2015 at 22:10 Comment(2)
I am not using auto layout. And if I change it now, it will be a huge change.Candra
You need to use auto layouts which will adjust the gap.Regardful
R
3

Because UITableViewCells are reused, you will find that when adding objects to the contentView, those objects will also be reused without being removed, leading to overlapping subviews. The way around this it to start your cellForRowAtIndexPath with the following code:

 static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    for (UIView * view in [cell.contentView subviews]) {
//        clears the cell before reusing
        [view removeFromSuperview];
    }

Hope this helps.

Regeneration answered 18/12, 2015 at 10:35 Comment(0)
J
2

Are you using auto layout? I have done this before several times with UITableViewCells though it’s mainly been with UILabel and not UITextView.

Your problem might be resolved with auto layout. You layout all your subviews (inside of UITableViewCell) relative to each other. If one view (i.e. UITextView) changes size the other views will then adjust relative to that view. Here are a few useful links.

This isn’t specific to UITableViewCell but has a lot of good examples of various scenarios. Stanford University Developing iOS 7 Apps: Lecture 9 - Animation and Autolayout https://www.youtube.com/watch?v=r1sc7NI6-wo

Jennings answered 9/12, 2015 at 21:46 Comment(4)
I am not using auto layout. And if I change it now, it will be a huge change.Candra
I understand some view may be too tough to switch over to autolayout. Keep in mind that you can use springs and struts together with autolayout. It doesn't have to stricktly be one or the other. You can also try implementing a custom UIView (.xib) which does autolayout but is a child to a view using springs and struts to isolate how you do the layout.Jennings
I am creating UITableViewCell subviews from code in cellForRowAtIndex and not through xib.Candra
And I am doing so because cell's UI depends on server data.Candra

© 2022 - 2024 — McMap. All rights reserved.