Comments (4)
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.
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.
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.
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)
- How to perform 3D touch on collectionViews inside of TableViewCells HOT 2
- deleagte and datasource are nil HOT 1
- I was wondering how you would do this implementation without a TableviewController, but instead with a ViewController, with a tableview inside? HOT 1
- Self-sizing Cells HOT 3
- TableViewCell.swift HOT 2
- Count is performed before data is loaded HOT 2
- Custom collectionview cell content HOT 2
- collection view datasource mthod not getting called HOT 4
- collectionView cellForItemAt method is not called for xib HOT 2
- How to use Table view section id in the CollectionView Cell while deque the collection cell
- How to use Table view section id in the CollectionView Cell while deque the collection cell HOT 2
- NSInternalInconsistencyException iOS12
- setCollectionViewDataSourceDelegate in cellForRowAt instead of willDisplay? HOT 1
- Falling back to 0 when setting content offset doesn't work when there's contentInset
- I can't set the height and weight for collectionViewCell HOT 1
- Not getting collection view tag HOT 2
- You need to upgrade this from version 3 to at least 4. HOT 1
- Why should UICollectionViewDataSource be in UIViewController instead of UITableViewCell ? HOT 2
- Pagination with collection view vertical layout HOT 1
- Right To Left issue HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from collection-view-in-a-table-view-cell.