๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿงฏ Troubleshooting

[Cheliz] UICollectionView leading/trailingSwipeActions - Realm Read/Delete

 

  ๋ฌธ์ œ  

ํ™ˆ ํ™”๋ฉด์—์„œ ์ถ”ํ›„ grid ํ˜•ํƒœ๋กœ๋„ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด, ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ UICollectionView๋กœ ๊ตฌํ˜„ํ–ˆ์—ˆ๋‹ค. ๋ฐ€์–ด์„œ ์‚ญ์ œ ๊ธฐ๋Šฅ์€ ๋‹น์—ฐํžˆ ๋„ฃ์„ ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ๊ณ„์† ์ปฌ๋ ‰์…˜ ๋ทฐ๋กœ ๊ฐœ๋ฐœ ์ค‘,, ์•„๋ฟ”์‹ธ.. ๊นจ๋‹ฌ์•˜๋‹ค.. UICollectionView์—๋Š” trailingSwipeAction ๊ธฐ๋Šฅ์ด UITableView์ฒ˜๋Ÿผ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ”„๋กœํ† ์ฝœ ๋ฉ”์„œ๋“œ๊ฐ€ ์ œ๊ณต๋˜์ง€ ์•Š์Œ์„,,,,,,

 

 

 

 

  ํ•ด๊ฒฐ  

private func setCollectionViewCompositionalLayout() {
    var configuration = UICollectionLayoutListConfiguration(appearance: .plain)
    configuration.showsSeparators = true
    configuration.trailingSwipeActionsConfigurationProvider = { [unowned self] indexPath in
        let delete = UIContextualAction(style: .normal, title: nil) { action, view, actionPerformed in
            if let deleteCompletion = self.deleteCompletion {
                deleteCompletion(indexPath.row)
            }
            actionPerformed(true)
        }
        delete.image = UIImage(systemName: "trash.fill")
        return .init(actions: [delete])
    }
    
    let layout = UICollectionViewCompositionalLayout.list(using: configuration)
    collectionView.setCollectionViewLayout(layout, animated: true)
}

 

์šฐ์„  ๊ธฐ์กด๊ณผ ๊ฐ™์ด ๋ฆฌ์ŠคํŠธ์ฒ˜๋Ÿผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด, UICollectionLayoutListConfiguration๊ณผ UICollectionViewCompositionalLayout์„ ์‚ฌ์šฉํ•˜์—ฌ Collection View๋ฅผ Table View์ฒ˜๋Ÿผ ํ‘œํ˜„ํ•˜์˜€๋‹ค.

 

 

๊ทธ๋ฆฌ๊ณ  ์ด configuration์˜ ๊ธฐ๋ณธ ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, Table View์ฒ˜๋Ÿผ separators๋ฅผ ๋ณด์ด๊ฒŒ ํ•˜๊ฑฐ๋‚˜ leading/trailingSwipeActions์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 

์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฐ€์–ด์„œ ์‚ญ์ œ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์„ฑ๊ณตํ–ˆ๋‹ค..!!!

 

 

 

 

 

 

 

 

 

 

  +  

UICollectionViewCompositionalLayout์œผ๋กœ๋Š” sectionInset, minimumLineSpacing, minimumInteritemSpacing, itemSize ๋“ฑ์„ ์ง€์ •ํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.

Collection View Cell ๋‚ด๋ถ€์˜ ๊ฐ์ฒด๋“ค์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ์…€ ํฌ๊ธฐ๊ฐ€ ์ง€์ •๋˜๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค.

 

posterImageView.snp.makeConstraints { make in
    make.leading.equalTo(checkButton.snp.trailing).offset(8)
    make.centerY.equalToSuperview()
    make.height.equalToSuperview().multipliedBy(0.8)
    make.width.equalTo(posterImageView.snp.height).multipliedBy(0.67)
}

๊ธฐ์กด - UICollectionViewFlowLayout ์ ์šฉ ์‹œ ์…€ ๋‚ด๋ถ€ ์˜ค๋ธŒ์ ํŠธ ๋ ˆ์ด์•„์›ƒ ์ง€์ •

 

 

 

posterImageView.snp.makeConstraints { make in
    make.leading.equalTo(checkButton.snp.trailing).offset(8)
    make.centerY.equalToSuperview()
    let screenHeight = UIScreen.main.bounds.height
    let imageHeight = screenHeight * 0.12
    make.height.equalTo(imageHeight)
    make.width.equalTo(posterImageView.snp.height).multipliedBy(0.67)
}

UICollectionViewCompositionalLayout ์ ์šฉ ์‹œ ์…€ ๋‚ด๋ถ€ ์˜ค๋ธŒ์ ํŠธ ๋ ˆ์ด์•„์›ƒ 

 

 

 

๋”ฐ๋ผ์„œ, ์…€ ๋‚ด๋ถ€ ๊ฐ์ฒด์˜ ํฌ๊ธฐ ์ง€์ •์„ ์›ํ•  ๊ฒฝ์šฐ, Superview์— ์˜์กดํ•˜์ง€ ์•Š๋„๋ก ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ ˆ์ด์•„์›ƒ ์ด์Šˆ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋Š”๋ฐ, ๋งž๋Š” ๋ฐฉ๋ฒ•์ธ์ง€ ํ™•์ธ์ด ํ•„์š”ํ•˜๋‹ค,,