【问题标题】:UICollectionViewCell content not stretchedUICollectionViewCell 内容未拉伸
【发布时间】:2018-05-01 13:17:40
【问题描述】:

我将UICollectionView 拉伸到整个屏幕宽度。我的UICollectionViewCells 是从MyViewCell.xib 加载的。我通过以下方式设置其大小:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
   return CGSizeMake(CGRectGetWidth(collectionView.frame) * 0.5, CGRectGetHeight(collectionView.frame));
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 16;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
    return cell;  
}

在主视图控制器加载期间,我有:

[self.collView registerNib:[UINib nibWithNibName:@"MyViewCell" bundle:nil] forCellWithReuseIdentifier:@"myCell"];
self.collView.delegate = self;
self.collView.dataSource = self;

这使“单元格视图”的大小正确,但从 XIB 加载的内容仍然“小”且未拉伸。这意味着,我可以在屏幕上看到两个项目,但它们不是 strechtech,仅向左对齐并且具有大约 50 的固定宽度。

如果我在 iPhone 上使用相同的代码,它可以正常工作,问题只出在 iPad 上。两台设备的 iOS 版本相同 (10.3.3)。

【问题讨论】:

  • 你能记录下你从layoutSubviews() 方法得到的frame 大小,看看你的单元格大小是否正确...
  • 调用似乎是正确的。如果我记录 collectionView.frame 的大小,即整个屏幕和正确的高度,则在约束中设置。
  • 在单元格本身中记录您从layoutSubviews() 获得的信息
  • 框架也是正确的,例如。与我在 sizeForItemAtIndexPath 中设置的相同
  • 你使用自动布局吗?

标签: ios objective-c uicollectionview uicollectionviewcell


【解决方案1】:

您的代码可以正常工作。我将它翻译成 Swift 如下(这是我的视图控制器代码的整个

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    @IBOutlet weak var cv: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.cv.register(UINib.init(nibName: "MyViewCell", bundle: nil), forCellWithReuseIdentifier: "cell")
        self.cv.delegate = self
        self.cv.dataSource = self
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 16
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = cv.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.layer.borderWidth = 2
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width:self.cv.bounds.width/2, height:self.cv.bounds.height)
    }
}

这是我在 iPad 模拟器上看到的:

鉴于默认的项目间距为 10,这正是我希望看到的。

MyViewCell nib 只包含一个黄色的小单元格。为了清楚起见,我添加了边框。

【讨论】:

  • 我尝试在单元格周围添加边框并且边框是正确的。所以问题出在单元格内容上,不像你的例子那样被拉伸。
  • 噢噢噢噢,这是一个很好的线索。所以你的 cell 有问题(可能在笔尖中)。我在笔尖中的单元格只是一个带有黄色背景色的普通 UICollectionViewCell。
  • 我已经清理了项目,重建,重置设备,它开始工作了。不过,感谢您的帮助。我没有想到带边框的把戏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 2010-11-23
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
相关资源
最近更新 更多