Cannot update UI object's frame in UITableViewCell

后端 未结 6 1971
眼角桃花
眼角桃花 2021-01-19 02:18

I have a subclassed UITableViewCell.

I need to dynamically change the frame of a UILabel.

Here\'s what I\'ve done:

- (UITableViewCell *)table         


        
相关标签:
6条回答
  • 2021-01-19 02:32

    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?

    0 讨论(0)
  • 2021-01-19 02:35

    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:)

    0 讨论(0)
  • 2021-01-19 02:42

    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.

    0 讨论(0)
  • 2021-01-19 02:46

    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)];
    
        ...
    
    0 讨论(0)
  • 2021-01-19 02:51

    You can not edit frame of UI object created through xib if Autolayout is on. There are two ways to do this:

    1. Disable Autolayout for that xib.

    OR

    1. Make an IBOutlet for particular NSlayout constraint attach it accordingly and update its constant property value through code and don't forget to call layOutIfNeeded appropriately on the object on which constarint is implemented or superview as per implementation.
    0 讨论(0)
  • 2021-01-19 02:51

    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;
        }
    
    0 讨论(0)
提交回复
热议问题