UICollectionViewCell 内容未拉伸
UICollectionViewCell content not stretched
我 UICollectionView
横跨整个屏幕宽度。我的 UICollectionViewCell
是从 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;
这使得 "cell view" 的大小正确,但是从 XIB 加载的内容仍然是 "small" 并且没有被拉伸。这意味着,我可以在屏幕上看到两个项目,但它们不是 strechtech,仅向左对齐并且宽度固定在 50 左右。
如果我在 iPhone 上使用相同的代码,它工作正常,问题仅出现在 iPad 上。两个设备具有相同的 iOS 版本 (10.3.3)。
您提供的代码可以正常工作。我将其翻译成 Swift 如下(这是我的视图控制器代码的 entirety:
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 笔尖仅包含一个黄色的小单元格。为了清楚起见,我添加了边框。
我 UICollectionView
横跨整个屏幕宽度。我的 UICollectionViewCell
是从 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;
这使得 "cell view" 的大小正确,但是从 XIB 加载的内容仍然是 "small" 并且没有被拉伸。这意味着,我可以在屏幕上看到两个项目,但它们不是 strechtech,仅向左对齐并且宽度固定在 50 左右。
如果我在 iPhone 上使用相同的代码,它工作正常,问题仅出现在 iPad 上。两个设备具有相同的 iOS 版本 (10.3.3)。
您提供的代码可以正常工作。我将其翻译成 Swift 如下(这是我的视图控制器代码的 entirety:
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 笔尖仅包含一个黄色的小单元格。为了清楚起见,我添加了边框。