Iphone UITableViewCell子类中的标签更改位置

Iphone UITableViewCell子类中的标签更改位置,iphone,uitableview,subclass,Iphone,Uitableview,Subclass,我在Interface Builder中制作了一个原型单元。这个原型包含一个imageview,它的大小应该根据字符串的长度而改变。 因此,我在UITableViewCell的子类中编写了一个方法,用给定字体计算文本字符串的宽度,并将此宽度设置为imageview的宽度。 这可以正常工作,直到我按下另一个视图控制器,然后返回到我的表视图。然后将视图重新定位到右侧约20个像素处 有人知道为什么会这样吗?如果我将x位置设置为静态,例如200,甚至会发生这种情况 更新1: 在我的-(UITableVi

我在Interface Builder中制作了一个原型单元。这个原型包含一个imageview,它的大小应该根据字符串的长度而改变。 因此,我在UITableViewCell的子类中编写了一个方法,用给定字体计算文本字符串的宽度,并将此宽度设置为imageview的宽度。 这可以正常工作,直到我按下另一个视图控制器,然后返回到我的表视图。然后将视图重新定位到右侧约20个像素处

有人知道为什么会这样吗?如果我将x位置设置为静态,例如200,甚至会发生这种情况

更新1:

在我的
-(UITableViewCell*)表视图中:cellforrowatinexpath:
我使用UITableViewCell的子类
DFolderCell
。这将调用方法
-(void)updateIndicator count:and starred
,该方法设置
count
starred
标签的值,并使这些标签和图像
countIcon
starredIcon
的宽度与
\u count
\u starredIcon>的宽度具有给定字体

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"folderCell";
    DFolderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        cell = [[DFolderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    int index = indexPath.row;
    Folder *folder = (Folder *) [folders objectAtIndex:index];
    FolderIcon icon = (FolderIcon) folder.icon.intValue;

    cell.name.text = folder.name;
    cell.icon.image = [[DFolders sharedFolders] imageForFolderIcon:icon];

    int count = [[DDrafts sharedDrafts] amountOfDraftsInFolder:folder withType:DraftLoadTypeRecent];
    int starred = [[DDrafts sharedDrafts] amountOfDraftsInFolder:folder withType:DraftLoadTypeStarred];
    [cell updateIndicatorsCount:count andStarred:starred];

    return cell;
}
下面的方法是我的子类
DFolderCell

/* Update size of count and starred indicators */
- (void)updateIndicatorsCount:(int)_count andStarred:(int)_starred
{
    UIFont *badgeFont = [UIFont systemFontOfSize:11.0];

    UIImage *background;

    _count = 1000; // Test

    if (_count > 0)
    {
        self.count.text = [NSString stringWithFormat:@"%i", _count];

        CGSize size = [self.count.text sizeWithFont:badgeFont];

        CGRect frame = self.countIcon.frame;
        frame.size.width = size.width;
        frame.origin.x = 320 - 20 - size.width;
        self.countIcon.frame = frame;
        self.count.frame = frame;

        background = [UIImage imageNamed:@"Folders_Badge_Count.png"];
        self.countIcon.image = [background stretchableImageWithLeftCapWidth:2 topCapHeight:2];

        self.count.hidden = NO;
        self.countIcon.hidden = NO;
    }

    /* Code for _starred is similar to code for _count */
}
这就是结果

这就是它的外观(以及在选择单元格之前的外观)

当一个单元格被选中时,一个新的viewcontroller被推到导航堆栈上,我通过返回弹出了这个view controller,这就是它的外观

更新2:

我在cell原型中保留了imageview,但注释掉了将其大小和图像设置为可拉伸图像的代码。我还将标签的背景色更改为纯红色,以查看标签的确切大小。尺寸似乎是正确的

这是我的
-(void)updateIndicatorsCount:andStarred:
方法

- (void)updateIndicatorsCount:(int)_count andStarred:(int)_starred
{
    UIFont *badgeFont = [UIFont systemFontOfSize:11.0];

    // UIImage *background;

    _count = 1000;

    if (_count > 0)
    {
        self.count.text = [NSString stringWithFormat:@"%i", _count];

        CGSize size = [self.count.text sizeWithFont:badgeFont];

        CGRect frame = self.countIcon.frame;
        frame.size.width = size.width;
        frame.origin.x = 320 - 30 - size.width;
        self.count.frame = frame;
        self.count.hidden = NO;
        self.count.backgroundColor = [UIColor redColor];

        /* DON'T DO ANYTHING TO THE IMAGE */
        /*
         background = [UIImage imageNamed:@"Folders_Badge_Count.png"];
         self.countIcon.hidden = NO;
         self.countIcon.image = [background stretchableImageWithLeftCapWidth:2 topCapHeight:2];
         self.countIcon.frame = frame;
        */
    } 
}
在推送到另一个视图控制器之前:

从视图控制器返回后:


单元格中使用此代码,它将解决您的问题

[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];

你能把代码显示在哪里吗?这是用什么方法完成的?@Jim我已将答案更新为“显示代码”。@occulus我已将答案更新为“显示两幅图像”(推之前和推之后,然后返回),我看到您正在为标签设置背景图像。这很好,但是对于这样一个问题,我会暂时取消它,只设置一个背景色,这样我就可以看到标签的实际大小(使用他们可能“不同意”的图像)。但我要说的是有趣的bug:)@makaron我没有为标签设置背景图像。标签下面是一个UIImageView。我曾尝试将此imageview保留在cell原型中,但删除了将其大小和图像设置为可拉伸图像的代码。我也尝试过给标签一个红色的背景色,以查看它的确切大小。尺寸似乎是正确的。我已经更新了我的问题,以显示现在的代码和应用程序的新屏幕截图。