I have a subclassed UITableViewCell.
I need to dynamically change the frame of a UILabel.
Here\'s what I\'ve done:
- (UITableViewCell *)table
There must be something wrong there.
Try, just for testing to implement this:
tableView:willDisplayCell:forRowAtIndexPath:
Like
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
MessageCell* aCell = (MessageCell*)cell;
aCell.Content.Text = @"Content!";
[aCell.Content setFrame:CGRectMake(0, 0, 10, 10)];
}
What's the result?
Add a subview to cell.Content
and resize that subview instead of the content itself. The cell will always be stretched to fit in your table view so you can't make it smaller.
(and of course to make the cell height smaller just apply tableView:heightForRowAtIndexPath:
)
Did you consider overriding the - (void) layoutSubviews
method in your custom cell (i.e. MessageCell) and layout your stuff in there? I think this is the safest place to put layout changing code in, because in other places it may not have any effect. Don't forget to call [super layoutSubviews];
in your implementation of layoutSubviews
.
Consider going with this approach and let us know if it worked out for you!
But you need to consider performance implications with this approach, because layoutSubviews
will be called every time the table scrolls.
EDIT:
to accommodate for the right frame size for your label inside layoutSubviews
consider using - (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
for the frame calculation.
Well a few days after posting the bounty I've found the answer, I hope this will help someone someday...
I am not 100% sure why, but what I tried to do simply can not be done:
You cannot change the frame of an object if you've put it through Interface Builder.
To make the changes I wanted to do, I simply created those objects programmatically in my subclass, inside "initWithCoder".
content = [[UILabel alloc] initWithFrame:CGRectMake(120, 100, 200, 50)];
content.opaque = NO;
content.numberOfLines = 0;
[self.contentView addSubview:content];
I was then able to change the frames inside "- (void)layoutSubviews" (but basically anywhere I wanted)
- (void)layoutSubviews
{
[super layoutSubviews];
[cell.Content setFrame:CGRectMake(0, 0, 10, 10)];
...
You can not edit frame of UI object created through xib if Autolayout is on. There are two ways to do this:
OR
May be this relates to fact of reusing cell. Try this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"messageCell";
MessageCell *cell = (MessageCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
cell = [[[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
}
cell.Content.Text = @"Content!";
[cell.Content setFrame:CGRectMake(0, 0, 10, 10)];
return cell;
}