let cell = BookmarkDetailCollectionViewCell.dequeueReusableCell(to: collectionView, indexPath: indexPath)
cell.bookmarkCellIndexPath = indexPath
return cell
self.bookmarkList.remove(at: indexPath.item)
collectionView.deleteItems(at: [indexPath])
๊ธฐ์กด ์ฝ๋๋ ViewController์์ cell์ indexPath ๊ฐ์ ๋๊ฒจ ์ฃผ์๋๋ฐ, ํด๋น cell์ ํด๋ก์ ๋ก ๋๊ฒจ ์ฃผ์์ ๊ฒฝ์ฐ ์ ๋์ ์ผ๋ก ๋ณํ๋ indexPath ๊ฐ์ด ์๋, ๊ณ ์ ์ ์ธ IndexPath ๊ฐ์ด๊ธฐ ๋๋ฌธ์, bookmarkList์ ๋ฐ์ดํฐ์ colelctionView์ item์ ๊ฐ๋ค์ด ๋ฌ๋ผ์ ์ฑ์ด ๊ฐ์ ์ข ๋ฃ ๋๋ ์ด์๊ฐ ์์์ต๋๋ค.
func getIndexPath() -> IndexPath? {
guard let superView = self.superview as? UICollectionView else { return nil }
return superView.indexPath(for: self)
}
ํด๋น cell์์์ ์์๋ทฐ์ธ superView์ ์ ๊ทผํด ํด๋น cell์ ์ ํํ IndexPath ๋ฐ์ดํฐ๋ฅผ ํด๋ก์ ๋ก ์ ๋ฌํด ์ ํํ indexPath์ ์ ๊ทผํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ ์ ์์์ต๋๋ค.
cell.bookmarkButtonClosure = { indexPath in
Task {
do {
try await BookmarkService.shared.postBookmark(BookmarkRequest(articleId: self.bookmarkList[indexPath.item].articleID,
bookmarkStatus: !self.bookmarkList[indexPath.item].bookmarked))
self.bookmarkList.remove(at: indexPath.item)
collectionView.deleteItems(at: [indexPath])
LHToast.show(message: "๋ถ๋งํฌ๊ฐ ํด์ ๋์์ต๋๋ค")
} catch {
guard let error = error as? NetworkError else { return }
self.handleError(error)
}
}
}
์ต์ ๋ ๋ฐ์ธ๋ฉ์ ์ํด ํด๋ก์ ์์์ guard let์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ฒ ๋ฐ์ธ๋ฉํ์ฌ ์ง์ญ ๋ณ์๋ฅผ ์ฌ์ฉํ์์ผ๋, ์ง์ญ ๋ณ์์ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๋ ๊ณผ์ ์์ reload ํ๋ ๊ณผ์ ์์ ๊ฐ์ ์ข ๋ฃ๊ฐ ๋์๋ค.
์ถํ์ ์ง์ญ ๋ณ์๊ฐ ์๋, ์ ์ญ ๋ณ์๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ๋ฐ์ ๋ถ๋งํฌ ๋ฐ์ดํฐ์ collectionView item์ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ฒ ๋๊ธฐํํ ์ ์์์ต๋๋ค.