更改UICollectionViewCell中的标签位置 [英] Changing label position in UICollectionViewCell

查看:105
本文介绍了更改UICollectionViewCell中的标签位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个UICollectionView,在情节提要中有一个原型单元.单元格中有一个UILabel("label"),其位置没有自动布局.

We have a UICollectionView which has a prototype cell on the Storyboard. The cell has a UILabel ("label") in it, which is positioned without autolayout.

我们使用setFrame在-collectionView:cellForItemAtIndexPath:中有条件地设置标签的框架,如下所示:

We set the frame of the label conditionally in the -collectionView:cellForItemAtIndexPath: with setFrame like this:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryCell *cell;
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Category" forIndexPath:indexPath];

    ...

    if (self.isViewTypeList) {            
        [((CategoryCell *)cell).label setFrame:FRAME_RECT];
    }

    ...

    return cell;
}

我们已经对其进行了检查,看来当它出队时,它首先会使用Storyboard值调用标签的-setFrame:,然后执行我们的代码,从而正确设置了新帧.除了这些以外,没有其他呼叫,但是标签保留在屏幕上的原始位置.

We have inspected it, and it seems that first of all it calls the label's -setFrame: with the Storyboard values when it gets to the dequeing, then our code follows, and this sets the new frames correctly. There is no other call other than these, but the label remains at it's original position on the screen.

怎么了?为什么新框架设置不正确?还有其他事吗?我必须重新播放或以某种方式刷新它吗?

What's wrong? Why is the new frame not set correctly? Is something other overrdiging it? Do I have to relayout or refresh it somehow?

推荐答案

请尝试执行以下步骤.这将为您服务-

Please try following steps. This will work for you-

  • 在情节提要或XIB文件中,选择原型单元格
  • 选择要更改框架的标签
  • Attributes Inspector (View Section)中设置其tag属性,就像设置tag = 101
  • In your storyboard or XIB file select prototype cell
  • Select the label for which you want to change frame
  • Set the tag property of it in Attributes Inspector (View Section), like set tag = 101

之后,在您的代码部分中执行此操作-

After that in your code section do this -

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//...
    CategoryCell *cell;
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Category" 
                                                     forIndexPath:indexPath];
    //...
    if (self.isViewTypeList) {
        UILabel *lbl = (UILabel *)[cell viewWithTag:101];
        [lbl setFrame:FRAME_RECT];
    }
    //...
}

祝你好运!

这篇关于更改UICollectionViewCell中的标签位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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