【问题标题】:Changing label position in UICollectionViewCell更改 UICollectionViewCell 中的标签位置
【发布时间】:2013-12-19 16:27:23
【问题描述】:

我们有一个UICollectionView,它在故事板上有一个原型单元。该单元格中有一个UILabel ("label"),它在没有自动布局的情况下定位。

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

- (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:,然后我们的代码如下,这将正确设置新帧。除了这些之外,没有其他调用,但标签仍保留在屏幕上的原始位置。

怎么了?为什么新框架设置不正确?是否有其他东西压倒它?我是否必须以某种方式重新布局或刷新它?

【问题讨论】:

    标签: ios objective-c uicollectionview frame


    【解决方案1】:

    从 xib 取消选中自动布局。改变框架可能会有所帮助。

    【讨论】:

      【解决方案2】:

      请尝试以下步骤。这对你有用-

      • 在故事板或 XIB 文件中选择原型单元
      • 选择要更改框架的标签
      • Attributes Inspector (View Section)中设置它的tag属性,如设置tag = 101

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

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

      祝你好运!

      【讨论】:

        【解决方案3】:

        尝试:

        - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                          cellForItemAtIndexPath:(NSIndexPath *)indexPath
        {
            //...
            //instead of
            //CategoryCell *cell;
            //cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Category" 
            //                                                 forIndexPath:indexPath];
            CategoryCell *cell = (CategoryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Category"
                                                                                           forIndexPath:indexPath];
            //sure reuse-identifier is "Category"?
        
            //...
            if (self.isViewTypeList) {
                //instead of
                //[((CategoryCell *)cell).label setFrame:FRAME_RECT];
                [cell.label setFrame:FRAME_RECT];
            }
            //...
            return cell;
        }
        

        还可以通过此链接查看您是否遗漏了一些关键步骤:
        http://ashfurrow.com/blog/uicollectionview-example

        【讨论】:

        • 设置单元格的其他属性(如背景颜色)可以正常工作,所以这不是问题。
        【解决方案4】:

        自动布局将覆盖您对setFrame: 的调用。 您可以使用标签的约束在单元格中添加一些出口,然后更改它们以更改框架。

        【讨论】:

        • 没有自动布局,也没有任何约束。
        猜你喜欢
        • 2011-12-20
        • 2020-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-02
        • 1970-01-01
        相关资源
        最近更新 更多