UICollectionViewCell содержимое не растягивается - PullRequest
0 голосов
/ 01 мая 2018

У меня 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;

Это делает «представление ячейки» правильного размера, но его содержимое, загруженное из XIB, все еще «мало» и не растянуто. Это означает, что я вижу два элемента на экране, но они не являются strechtech, только выровнены по левому краю и имеют фиксированную ширину около 50.

Если я использую тот же код на iPhone, он работает правильно, проблема только с iPad. Оба устройства имеют одинаковую версию iOS (10.3.3).

1 Ответ

0 голосов
/ 01 мая 2018

Ваш код, как указано, работает правильно. Я перевел его на 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:

enter image description here

И это, учитывая интервал по умолчанию между 10, это именно то, что я ожидал бы увидеть.

Перо MyViewCell содержит только небольшую желтую ячейку. Я добавил границу только для ясности.

...