UITableViewCell 子类中的标签更改位置

发布于 2024-12-20 20:36:01 字数 4145 浏览 1 评论 0原文

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

有谁知道为什么会发生这种情况?如果我将 x 位置设置为静态值(例如 200),甚至会发生这种情况。

更新 1:

在我的 - (UITableViewCell *)tableView:cellForRowAtIndexPath: 中,我使用我的子类UITableViewCell,DFolderCell。这会调用方法 - (void)updateIndicatorsCount:andStarred ,该方法设置 countstarred 标签的值,并使这些标签和图像countIconstarredIcon 的宽度与给定字体的 _count_starred 的宽度之比。

- (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 */
}

这是结果。

这是它应该始终呈现的样子(以及选择单元格之前的样子)

Before go back

当选择单元格时,一个新的视图控制器已被推送到导航堆栈上,我通过返回弹出了该视图控制器,这就是它的外观。

After go back

更新 2:

我已将 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;
        */
    } 
}

在推送到另一个视图控制器之前:

Before Push

从视图控制器返回后:

返回后

I have made a prototype cell in Interface Builder. This prototype contains an imageview which should change size depending on the length of a string.
Therefore I have written a method in my subclass of UITableViewCell to calculate the width of the text string with a given font and set this width to the width of the imageview.
This works fine until I push to another view controller and then go back to my table view. Then the view has been repositioned about 20 pixels to the right.

Does anyone know why this happens? This even happens if I set the x-position to something static, e.g. 200.

Update 1:

In my - (UITableViewCell *)tableView:cellForRowAtIndexPath: I use my subclass of UITableViewCell, DFolderCell. This calls the method - (void)updateIndicatorsCount:andStarred which sets the value of the count and the starred label and makes those labels and the images countIcon and starredIcon the width to the width of _count and _starred with a given font.

- (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;
}

The method below is part of my subclass 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 */
}

This is the result.

This is how it should always look (and how it looks before selecting a cell)

Before going back

When a cell has been selected, a new viewcontroller has been pushed onto the navigation stack and I have popped this view controller by going back, this is how it looks.

After going back

Update 2:

I have kept the imageview in the cell prototype but commented out the code setting its size and its image to a stretchable image. I have also changed the background color of the label to a solid red to see the exact size of the label. The size seems to be correct.

This is my - (void)updateIndicatorsCount:andStarred: method now.

- (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;
        */
    } 
}

Before pushing to another view controller:

Before pushing

After going back from the view controller:

After going back

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年已逝 2024-12-27 20:36:01

在您的 cellForRowatindexpath 中使用此代码它将解决您的问题

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

Use this code in your cellForRowatindexpath it will solve your issue

[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文