简体   繁体   中英

CollectionView crashes after HOME button pressed on iPads

I have a CollectionView and I set size for its cells dynamically. Here is the problem: when I hit 'home' button and enter the game again, then the CollectionView cracks. All the cell look awful, they have wrong size and appears in wrong place. And it happens only on iPads and works perfectly on iPhones. Here is my code for creating the table:

if collectionView == tableCollectionView{

        let cell:TableViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "letter_cell", for: indexPath) as! TableViewCell

        let size = Int(collectionView.frame.size.width) / (lettersCountInRow + 1);
        let j = indexPath.row
        let i = indexPath.section

        let letter = guessLogic!.letters[i * lettersCountInRow + j]
        let imageName = String(describing: letter)

        let vertical_shift = (Int(collectionView.frame.size.height) - size * 2) / 2
        let pad = Double(size) * 0.1;
        let start_x = Int(collectionView.frame.size.width) - size*7 - Int(pad) * lettersCountInRow;


        cell.letterImage.image = UIImage(named: imageName)
        cell.frame = CGRect(x:start_x + j * Int(pad) + j * size, y: i * size + vertical_shift + Int(pad), width: size, height: size)

        return cell

    }

Tested on iPad Air-2

This is how the table always should looks like: Normal look

And this how it looks like after I press HOME and return to the game: Wrong bullsh*t look

did you set the size of the cells in viewdidload() ? In that case try to do it in viewdidappear() and see.

Well, I've found the solution. It was because of orientation. I disabled landscape orientation and everything started working perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM