无法在UITableViewCell中更新UI对象的框架 [英] Cannot update UI object's frame in UITableViewCell

查看:91
本文介绍了无法在UITableViewCell中更新UI对象的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个子类UITableViewCell。

I have a subclassed UITableViewCell.

我需要动态更改UILabel的帧。

I need to dynamically change the frame of a UILabel.

这就是我所做的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"messageCell";
    MessageCell *cell = (MessageCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.Content.Text = @"Content!";
    [cell.Content setFrame:CGRectMake(0, 0, 10, 10)];
}

UILabel的文本确实发生了变化,因此这不是IBOutlet问题。
然而,框架仍然与IB中定义的相同。

The text of the UILabel does change so this is not an IBOutlet issue. The frame however remains the same as was defined in IB.

之前我做过这样的事情所以我不确定这里出了什么问题...

I've done stuff like this before so I'm not really sure what went wrong here...

感谢您的帮助。

修改:

我使用

[self.tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil]
     forCellReuseIdentifier:@"MessageCell"]; 

在viewDidLoad中

in viewDidLoad

推荐答案

在发布赏金后的几天我找到了答案,我希望有一天能帮到某人...

Well a few days after posting the bounty I've found the answer, I hope this will help someone someday...

我不是100 %确定原因,但我试图做的事情根本无法完成:

I am not 100% sure why, but what I tried to do simply can not be done:

如果您通过接口放置它,则无法更改对象的框架生成器。

为了进行我想做的更改,我只是在子类中以initWithCoder编程方式创建了这些对象。

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];

然后我可以更改 - (void)layoutSubviews中的框架(但基本上我在任何地方)希望)

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)];

    ...

这篇关于无法在UITableViewCell中更新UI对象的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆