iphone - 无法在 UITableViewCell 中更新 UI 对象的框架

标签 iphone objective-c uitableview

我有一个子类化的 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/

相关文章:

ios - 使用部分时继续详细了解 UItableView

ios - PFUser CurrentUser 的 Parse.com 查询

iphone - iOS 动画启动画面

iphone - iOS SDK写在UIImage上绘制文本

iphone - 导航 Controller 的 leftBarButtonItem 中有多个按钮

iPhone 连通性测试 : How do I force it to lose connection?

ios - 如何在 iOS 的 UITableViewCell 中播放视频(自动播放)

ios - 执行 segue 时 View 出现两次

iPhone iOS 的图像调整大小以实现最低内存使用率解决方案

c++ - 在 C++ 项目中使用 (OSX) Core MIDI