【问题标题】:UICollectionView Headers OverlappingUICollectionView 标题重叠
【发布时间】:2015-06-01 18:22:15
【问题描述】:

我有一个带有以下相关方法的collectionView:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {

    if (kind == UICollectionElementKindSectionHeader) {
    Scoreboard *thisScoreboard = [self.scoreboards objectAtIndex:indexPath.section];

    ScoreboardReusableView *view = nil;

    view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                              withReuseIdentifier:@"scoreboardHeader"
                                                     forIndexPath:indexPath];

    //view.backgroundColor = [UIColor yellowColor];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
    //label.text = [NSString stringWithFormat:@"%@ vs %@", thisScoreboard.awayteam, thisScoreboard.hometeam];
    label.text = [NSString stringWithFormat:@"%ld", (long)indexPath.section];


    [view addSubview:label];

    return view;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 34);
}

但是,标签文本是重叠的。这是collectionView的两个截图。第 0 节和第 1 节(注意文本不重叠):

第 2 节和第 3 节(文本在此处和所有后续节标题上重叠):

我的标题 xib ScoreboardReusableView 完全空白,因为无论我在其中放入什么(例如,UILabel 或 UIImageView,它都不会显示在 UICollectionView 标题中)。最好我会在 xib 中设计标题,但由于这不起作用,我尝试以编程方式进行,但现在标签重叠。

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout


    【解决方案1】:

    集合视图重复使用单元格和标题。当标题滚动到屏幕外时,它会被添加到队列中,并将被重新用于下一个要在屏幕上滚动的标题。这意味着您在 collectionView:viewForSupplementaryElementOfKind: 方法中的配置代码将在不同 indexPaths 的同一标题视图上再次运行。由于您只是在每次配置代码运行时添加一个标签,因此它们将一个堆叠在另一个之上。

    正确的做法是在headerView上有一个label属性,这样就可以改变它的文字了。但是……

    无论我在其中放入什么(例如,UILabel 或 UIImageView),它都不会显示

    这是你真正的问题。您是否在 xib 上启用了自动布局?如果是这样,请务必为标签添加约束,否则它将具有零宽度和高度,从而产生您描述的效果。

    【讨论】:

      【解决方案2】:

      Swift 3 版本,希望对你有帮助

      func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
          var reusableview: UICollectionReusableView?
          reusableview = nil
          reusableview = playersCollectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "collViewHeader", for: indexPath as IndexPath)
          if kind == UICollectionElementKindSectionHeader {
      
              //remove all previous subviews:
              if let views = reusableview?.subviews {
                  for view in views {
                      view.removeFromSuperview()
                  }
              }
              //----------
      
              if reusableview == nil {
                  reusableview = UICollectionReusableView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 50))
              }
      
              //init label
              let label = UILabel.init(frame: CGRect(x: 10.0, y: 20.0, width: self.view.frame.width-20, height: 25.0)) //-20 from width to make it no go out of screen bounds
              label.textColor = UIColor.blue //you may set your color here :)
              label.adjustsFontSizeToFitWidth = true
              label.text = "your text here"
      
              reusableview?.addSubview(label)
          }
          return reusableview!
      }
      

      【讨论】:

        【解决方案3】:

        ScoreboardReusableView 为空白,但可重复使用。您必须将 UILabel 放入页眉 xib 中,然后为其设置文本。

        例如:

        - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
        
            if (kind == UICollectionElementKindSectionHeader) {
                Scoreboard *thisScoreboard = [self.scoreboards objectAtIndex:indexPath.section];
        
                ScoreboardReusableView *view = nil;
        
                view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                  withReuseIdentifier:@"scoreboardHeader"
                                                         forIndexPath:indexPath];
        
                view.label.text = [NSString stringWithFormat:@"%ld", (long)indexPath.section];
        
                return view;
            }
            return nil;
        }
        

        【讨论】:

        • 当我将 UILabel 放入 ScoreboardReusableView 并设置该标签属性的文本时, UILabel 根本不会出现。如果我设置 xib 的背景颜色,它也不会出现。
        【解决方案4】:
        - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
        {
            if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
            UICollectionReusableView *reusableview=nil;
            reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        
            NSArray *viewsToRemove = [reusableview subviews];
            for (UIView *v in viewsToRemove) {
                [v removeFromSuperview];
            }
        
            if (reusableview==nil) {
                reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30)];
            }
            reusableview.backgroundColor=[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0];
        
            UILabel *lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width-90, 30)];
            [lblTitle setFont:[UIFont fontWithName:@"Ubuntu-Light" size:12]];
        
            lblTitle.textColor=HTAtHomeColor;
            [reusableview addSubview:lblTitle];
        
            UILabel *lblCount=[[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-80, 0, 70, 30)];
            lblCount.textAlignment=NSTextAlignmentRight;
            lblCount.textColor=[UIColor blackColor];
            [lblCount setFont:[UIFont fontWithName:@"Ubuntu-Light" size:12]];
             lblCount.text=[NSString stringWithFormat:@"%d Section Header",indexPath.row];
        
        
            [reusableview addSubview:lblCount];
        
            return reusableview;
        }
        return nil;
        }
        

        【讨论】:

        • 此代码将解决集合视图中的标题重叠问题
        • 感谢它对我帮助很大,但我需要将其重写为 swit)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-01
        相关资源
        最近更新 更多