だけクリックされたボタンに基づいてコレクションビューをリロードする必要がisBookMarkClikedブール を取る:より良いブール
Readablityは書籍のモデルを作成する のように
class Book {
var title: String
var author: String
var isBookMarked:Bool
init(title: String, author: String, isBookMarked:Bool) {
self.title = title
self.author = author
self.isBookMarked = isBookMarked
}
}
、はい、彼らは同じだ延長
extension YourClassVC: UICollectionViewDataSource,UICollectionViewDelegate
{
//MARK: UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if isBookMarkClicked
{
return arrForBookMarkedBooks.count
}
return arrForBooks.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellIdentifier", for: indexPath) as! YourCellClass
var currentBook:Book = nil
if isBookMarkClicked
currentBook = arrForStoreDetails[indexPath.row]
else
currentBook = arrForBookMarkedBooks[indexPath.row]
//Set data to cell from currentBook
return cell
}
//MARK: UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: false)
//Your code to push BookDetailVC
}
}
を使用してCollectionViewDelegateメソッドを作成しブックモデルでグローバルに
を2つの配列を宣言。しかし、私は別々のvcでそれらを示したい。だから私はbookvcからbookmarkvcを継承しようとしています。 – noob
サブクラスする必要はありません。たとえば、このBooksVCだけで新しいストーリーボードを作成し、プログラマチックにインスタンス化し、必要なときにいつでも表示することができます。デリゲートを設定するためにフラグなどを使用します。添付のコードを参照してください。 – Pochi
@noob同じデータのためにVCを分離するのは良いことではありません。ブックのモデルを作成するソリューションを掲載していますので、どのブックがbookMarkedであるか簡単に識別できます –