Giter VIP home page Giter VIP logo

Comments (4)

ashfurrow avatar ashfurrow commented on June 19, 2024

Hey there! I'm glad you found my tutorial helpful! I think I understand the issue, but for things like this it's always a good idea to attach a screenshot πŸ˜‰

So the problem seems to be putting the right image on the right collect view cell. Sounds like a problem with cellForItemAtIndexPath, which configures the cells. If I had to guess, rowItem isn't being assigned correctly. Not sure what representativeItem is, but indexPath.item isn't being used anywhere, which is a problem the row and item together define each cell, so you need both to configure a cell.

Let me know if that makes sense!

from collection-view-in-a-table-view-cell.

ashfurrow avatar ashfurrow commented on June 19, 2024

Oh, and collection views are pretty not-straightforward. If you're new to them, it might be worth going through a few other tutorials just for practice. This is a really great one I recommend.

from collection-view-in-a-table-view-cell.

cmag0505 avatar cmag0505 commented on June 19, 2024

Hi,

and thanks so much for your answer.

As I said before, I am trying to build an audiobook player.

I imagined the library as a sort of dropdown menu with to custom cells.
The artist names and below in the dropdown a collection view with all the books.
It is a pretty simple query I use for that. The representativeItem is from the query.

I figured out the dropdown, and that all works fine. But I can’t figure out how to get the books right.

I made a simple tableview to collectionview player before (the tableview showed the artists, the collectionview the books), and it worked fine.

Below is my complete code. Any help would be very appreciated.

Thanks so much.
Cosima

Am 23.11.2015 um 00:19 schrieb Ash Furrow [email protected]:

This http://www.raywenderlich.com/78550/beginning-ios-collection-views-swift-part-1

    override func viewDidLoad() {
        super.viewDidLoad()


        let query = MPMediaQuery.audiobooksQuery()
        query.groupingType = MPMediaGrouping.Artist
        collection = query.collections!
        query.groupingType = MPMediaGrouping.Artist

        print(books)

        }

        // Do any additional setup after loading the view.

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }





    // MARK - TABLE VIEW HEADER

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return collection.count
    }

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 80
    }

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! CustomHeaderCell

        let rowItem = collection[section].representativeItem

        headerCell.artistLabel.text = rowItem?.artist

        let headerView = HeaderView(tableView: self.tableView, section: section)

        headerView.addSubview(headerCell)

        return headerView
    }




    // MARK - TABLE VIEW CELL

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if (!collection.isEmpty) {
            if (self.tableView.sectionOpen != NSNotFound && section == self.tableView.sectionOpen) {
                return 1
            }
        }
        return 0
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("AlbumCell", forIndexPath: indexPath)

//        let rowItem = collection[indexPath.row].representativeItem
//        
//        let artwork = rowItem!.valueForProperty(MPMediaItemPropertyArtwork) as! MPMediaItemArtwork
//        albumImage = artwork.imageWithSize(cell.imageView!.bounds.size)
//        
//        cell.imageView?.image = albumImage


        return cell
    }

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

        guard let tableViewCell = cell as? CustomAlbumCell else { return }

        tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
        tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0

    }

    func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

        guard let tableViewCell = cell as? CustomAlbumCell else { return }

        storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
    }


    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    }



    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}

extension MainController: UICollectionViewDelegate, UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return books.count
    }


    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ArtworkCell", forIndexPath: indexPath) as! AlbumCoverCell

        let rowItem = collection[collectionView.tag][indexPath.item]

        var artist = (rowItem?.artist)! as String

        let query = MPMediaQuery.audiobooksQuery()
        let predicate = MPMediaPropertyPredicate(value: artist, forProperty: MPMediaItemPropertyArtist, comparisonType: .EqualTo)
        query.addFilterPredicate(predicate)
        query.groupingType = MPMediaGrouping.Album

        books = query.collections!

        let rowItem1 = books[indexPath.section].representativeItem

        let artwork = rowItem1!.valueForProperty(MPMediaItemPropertyArtwork) as! MPMediaItemArtwork
        albumImage = artwork.imageWithSize(cell.albumCover.bounds.size)

        cell.albumCover.image = albumImage

        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
    }
}

from collection-view-in-a-table-view-cell.

ashfurrow avatar ashfurrow commented on June 19, 2024

I mean, I'm not sure, but I think the problem is with this line:

let rowItem1 = books[indexPath.section].representativeItem

You're asking for a representative item for each table view cell, but you want the individual item instead. Each cell within the collection view (I think) has the same representative item, so using that here makes all the cells have the same artwork. Getting rid of rowItem1 and just using rowItem should work, I think.

Again, it's hard to debug someone's app over GitHub issues πŸ˜‰ But good luck!

from collection-view-in-a-table-view-cell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.