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.
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).
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.
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.
来源:https://stackoverflow.com/questions/25911484/ios-8-cell-resizing