我有一个子类化的 UITableViewCell。
我需要动态更改 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 中定义的相同。
我以前做过这样的事情,所以我不太确定这里出了什么问题......
感谢您的帮助。
编辑:
我用
[self.tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil]
forCellReuseIdentifier:@"MessageCell"];
在viewDidLoad中
最佳答案
在发布赏金几天后我找到了答案,我希望有一天这能对某人有所帮助......
我不是 100% 确定为什么,但我尝试做的事情根本无法完成:
如果通过 Interface Builder 放置对象,则无法更改对象的框架。
为了进行我想做的更改,我只是在我的子类中的“initWithCoder”中以编程方式创建了这些对象。
content = [[UILabel alloc] initWithFrame:CGRectMake(120, 100, 200, 50)];
content.opaque = NO;
content.numberOfLines = 0;
[self.contentView addSubview:content];
然后我可以更改“- (void)layoutSubviews”中的框架(但基本上是我想要的任何地方)
- (void)layoutSubviews
{
[super layoutSubviews];
[cell.Content setFrame:CGRectMake(0, 0, 10, 10)];
...
关于iphone - 无法在 UITableViewCell 中更新 UI 对象的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12872987/