3

I have a subclass of a UITableViewCell and I override setFrame: to add insets to cell. In iOS 7 everything seems to work fine, on iOS 8 on the other hand, the table view loads the cells correctly but when I refresh the tableview the cells are resized. By commenting the code in the setFrame the tableview behaves correctly, but I also loose the insets. Has anyone experienced this?

UPDATE: AFAIK, in iOS 8 the cell frame height matches the row height.

Mihai Popa
  • 892
  • 1
  • 8
  • 25
  • I havent experienced this, but there is a very good article on cell resizing, I hope it helps http://www.appcoda.com/self-sizing-cells/ – DevC Sep 18 '14 at 12:42
  • I'm experiencing the same behavior. When I toggle my tableview between editing and normal mode the insets are added on the frame.size.width, so the tablecell is getting smaller each time I switch to edit mode. – HashtagMarkus Sep 18 '14 at 13:51

3 Answers3

4

i got similar problerm.

solve it by:

- (void)setFrame:(CGRect)frame
{
  NSInteger inset = kDefaultViewPadding;
  frame.origin.x += inset;
  frame.size.width = kMainScreenWidth - 2*inset;

  [super setFrame:frame];
}

where

#define kMainScreenWidth            ([[UIScreen mainScreen] bounds].size.width)

,

don't use code like:

    frame.origin.x += TABLE_PADDING;

or

    frame.size.width = self.superview.frame.size.width - (2.0f * TABLE_PADDING);

otherwise, every time refresh tableview , the cell's width reduce (2.0f * TABLE_PADDING).

phenmod
  • 141
  • 2
  • 6
3

I don't know why the behavior changed in iOS 8 but I found a workaround.

In my case I changed the setFrame message to the following:

-(void) setFrame:(CGRect) frame {
    frame.origin.x += TABLE_PADDING;
    frame.size.width = self.superview.frame.size.width - (2.0f * TABLE_PADDING);
    [super setFrame: frame];
}

So I'm using the superview boundings for calculation. However this might not work in any case...

I ran into another problem while reordering the cell.

In case you are reordering try this:

-(void) setFrame:(CGRect) frame {

    if(isDragging)
        frame.origin.x = self.supervire.frame.origin.x + TABLE_PADDING;
    else
        frame.origin.x += TABLE_PADDING;

    frame.size.width = self.superview.frame.size.width - (2.0f * TABLE_PADDING);
    [super setFrame: frame];
}

To get the information if the TableCell is dragging I used the solution provided in this question: How to get notified of UITableViewCell move start and end

Edit I ran into more and more problems with the setFrame message, so I decided NOT to use the standard TableViewController. Now I'm using a standard ViewController with a searchbar and a search display controller. And a table View inside the controllers View. That way I can use autolayout constraints to adjust the table width. This seems to be the best solution for my case since I could not find any way to get it to work (in any case) using the setFrame message.

Community
  • 1
  • 1
HashtagMarkus
  • 1,641
  • 11
  • 20
  • I've run into this same issue, but I'm also setting the frame height. It's still getting shrunk upon reload. I can't use the superview.frame because that would be the height of the entire screen. How could I go about this? – BlueBear Oct 22 '14 at 02:54
0

I have partially solved my problem by moving the code from setFrame to layoutSubviews and instead of using self.frame, I use a property saved in awakeFromNib where I keep my bounds:

- (void)layoutSubviews
{
[super layoutSubviews];
    CGRect bounds = self.cellBounds;
    CGRect boundsWithInsets = CGRectMake(bounds.origin.x + horizontalPadding,
                                        bounds.origin.y + verticalPadding,
                                        bounds.size.width - 2 * horizontalPadding,
                                        bounds.size.height - 2 * verticalPadding);
    [super setBounds:boundsWithInsets];
}

The problem arises again when I swipe to enter in editing mode, and all my cells loose their left padding (as mentioned by @Zerd1984)

I should also mention that I use TLIndexPathTools library which currently is not updated to solve this issue.

Mihai Popa
  • 892
  • 1
  • 8
  • 25