Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:] - ios

I am creating a table view and inside a custom UItableviewCell I am using collection view and I also create a custom collectionview cell. After setting all the basic functionalities of collection view in TableViewCell, When run the app I got the crash with reason:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:cellForItemAtIndexPath: ( {length = 2, path = 0 - 0}) was not retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath: or is nil (>)'
I tried to search more for it but can't find any direction
Here is my code snippet:
1. In TableViewcell awakeFrom Nib:
override func awakeFromNib() {
super.awakeFromNib()
// self.collectionView_Files.registerNib(UINib(nibName: "MediaCollectionCell", bundle: nil), forCellWithReuseIdentifier: "MediaCollectionCell")
self.collectionView_Files.registerClass(MediaCollectionCell.self, forCellWithReuseIdentifier: "MediaCollectionCell")
}
CollectionView methods:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrFolderData.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let simpleTableIdentifier = "MediaCollectionCell"
var cell: MediaCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier(simpleTableIdentifier, forIndexPath: indexPath) as! MediaCollectionCell
cell = NSBundle.mainBundle().loadNibNamed(simpleTableIdentifier, owner: self, options: nil)[0] as! (MediaCollectionCell)
let dict = arrFolderData[indexPath.row]
if(dict["file_type"] as! String == "0") { /// Means image
cell.imgView_Item.image = UIImage(named: "images_41")
cell.btn_ViewImage.hidden = false
cell.btn_PlayVideo.hidden = true
} else if (dict["file_type"] as! String == "1") { /// Means video
cell.btn_ViewImage.hidden = true
cell.btn_PlayVideo.hidden = false
cell.imgView_Item.image = UIImage(named: "video_thumb_icon")
}
// cell.backgroundColor = arrFolderData[indexPath.item]
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Collection view at row \(collectionView.tag) selected index path \(indexPath)")
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
let length = (UIScreen.mainScreen().bounds.width-15)/2
return CGSizeMake(length,length);
}

As mentioned in the post, I was working in tablecell xib file and try to do as I need, however once I change my approach and I create tableview cell into the UITableView inside the storyboard and update all the outlets and then run the app. The app was working fine....all the outlets in collection view cell are dynamically created with taking reference from their static variables.

Related

Assertion failure in UICollectionView inside UITableViewCell

My code has a UICollectionView inside a UITableViewCell. I have two different set of arrays named TopBrandArray & TopCategoryArray - which is being displayed in two different cells of a Table inside a collectionView.
The delegates and datasource has been connected to the HomeViewController in the main storyboard. Also, the name for the cells has been given.
There is no error while compiling the program but gets an expection while running the code.
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier TopCategoryCollectionID - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
When I am trying to run by displaying only one single cell of the tableviewcell, the code works fine. But if am adding more cell the above mentioned error pops up. Here,I am adding the code:
#IBOutlet weak var homePageTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.homePageTable.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "TopCategoryID", for: indexPath) as! TopCategoriesTableViewCell
cell.tablecollection1.reloadData()
return cell
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: "BrandTableID", for: indexPath) as! BrandsTableViewCell
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 162
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0{
return topCategoryArray.count
}
else{
return topBrandArray.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TopCategoryCollectionID", for: indexPath) as? TopCategoriesCollectionViewCell{
cell.cat_image.image = UIImage(named: topCategoryArray[indexPath.row].image)
print(topCategoryArray[indexPath.row].name)
cell.cat_value.text = topCategoryArray[indexPath.row].name
return cell
}
else{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BrandsCollectionID", for: indexPath) as! BrandsCollectionViewCell
cell.layer.borderWidth = 1.5
cell.layer.borderColor = UIColor.gray.cgColor
cell.layer.cornerRadius = 4
cell.brandimage.image = UIImage(named: topBrandArray[indexPath.row].image)
return cell
}
}
}
Since it is asking to register a nib or a class for the identifier, I do not know where inside the code should I write the code because when I wrote it in viewDidload(), an error popped up showing "Ambiguous reference to member collectionView(_:numberOfItemsInSection:)"
The issue is with the registration of the cell with collection view. The collection view is unable to dequeue cell with the "TopCategoryCollectionID" identifier. You need to say to collection view that I'm going to use this cell as collection view cell.
You need to register TopCategoryCollectionID cell to collection view.
let nib = UINib(nibName: "TopCategoryCollectionID", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "TopCategoryCollectionID")
If you have used cell in storyboard then no need to register cell again.
EDIT
As you have collection view inside your tableview cell so datasource and delegate of collection view must be implemented indie the respective cells.
You need to transfer the collection view code into table view cells.

Moving UICollectionViewCell causes Error for invalidateItemsAtIndexPaths

When dragging to move cells in a UICollectionView, the app is crashing due to items being marked as invalidated, but then missing at the index path.
The code successfully confirms performs the move in the collection. Then the collection returns the expected number of sections and the cells for the section. After seemingly completing without issue, the following error occurs:
2018-12-20 15:39:54.216391-0500 TestApp[1748:485235] *** Assertion failure in -[UICollectionViewData invalidateItemsAtIndexPaths:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3698.93.8/UICollectionViewData.m:166
2018-12-20 15:39:54.216878-0500 TestApp[1748:485235] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempting to invalidate an item at an invalid indexPath: {length = 2, path = 0 - 1} globalIndex: 1 numItems: 0'
The code itself isn't doing anything very interesting. It makes sure that the last cell in edit mode (an add button) isn't moved, and then updates the data for our model.
It doesn't seem like any of the items in the collection should be invalidated in the first place. The number of items in the collection is fairly small, so everything is onscreen the entire time so no cells are being removed from view in this scenario. No content for any of the cells was changed, so I'm not entirely sure what could have been marked as invalid, but even with that occurring, I'm unaware why it would be missing.
Code below:
// MARK: UICollectionViewDataSource Functions
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let friendsCount = AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count
if friendsCount >= 0 {
return isEditingEnabled ? friendsCount + 1 : friendsCount
} else {
return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if isEditingEnabled && indexPath.row == AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count {
if !isUpgraded {
var currentVideoCount = 0
for i in 0..<AppDataModel.sharedInstance.groupModels.count {
currentVideoCount += AppDataModel.sharedInstance.groupModels[i].friends.count
}
if currentVideoCount >= maxFreeVideos {
print("Max Videos for trial reached.")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "upgradeCell", for: indexPath)
return cell
}
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addFriendCell", for: indexPath)
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "friendCell", for: indexPath) as! MainViewFriendCollectionViewCell
let friendForCell = AppDataModel.sharedInstance.groupModels[groupIndex!].friends[(indexPath as NSIndexPath).row]
cell.setup(friendForCell, shouldHideDeleteButton: !isEditingEnabled, onDeleteFriend: self.onDeleteFriend, onForceUpload: self.onForceUpload)
return cell
}
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// The user cannot reorder the add button
let addButtonIndex = AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count
let destinationIndex = (destinationIndexPath.item >= addButtonIndex) ? addButtonIndex - 1 : (destinationIndexPath as NSIndexPath).item
// reflect the changes to the view in the model
AppDataModel.sharedInstance.groupModels[groupIndex!].updateFriendOrdersAfterReorder((sourceIndexPath as NSIndexPath).item, toIndex: destinationIndex)
}
func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
let addButtonIndex = AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count
return proposedIndexPath.item >= addButtonIndex ? originalIndexPath : proposedIndexPath
}

How do I got Multiple Selections in UICollection View using Swift 4

I'm new to swift and building iOS Application from the scratch (using swift 4) and want to do something like below.
1. Implement Multiple cell selections in UICollectionView,
2. Pass selected cells data to Server.
Please anyone can help me, how to do that? Tell me the process and supporting articles to do that.
Below is reference Image. Thanks in Advance.
Well, the best way to handle multiple selections in UICollectionView
Enable Multiple Selection
myCollectionView.allowsMultipleSelection = true
put this code in your cell awakeFromNib
override func awakeFromNib() {
super.awakeFromNib()
let view = UIView(frame: bounds)
self.backgroundView = view
let coloredView = UIView(frame: bounds)
coloredView.backgroundColor = UIColor.red
self.selectedBackgroundView = coloredView
}
you can get the selected indexPath items
let items = myCollectionView.indexPathsForSelectedItems
This basic example. You can change as per your data.
When you select any cell then you need to check that selected cell is already selected before or not.
If not then add selected cell indexPath in indexArray and selected cell value in valueArray.
If current selected cell is previously selected then remove indexPath from indexArray and also remove selected cell value from valueArray
on continue button press pass arrSelectedData to server or next screen.
Define below 3 array.
var arrData = [String]() // This is your data array
var arrSelectedIndex = [IndexPath]() // This is selected cell Index array
var arrSelectedData = [String]() // This is selected cell data array
//UICollectionView Delegate & DataSource
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.arrData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
if arrSelectedIndex.contains(indexPath) { // You need to check wether selected index array contain current index if yes then change the color
cell.vw.backgroundColor = UIColor.red
}
else {
cell.vw.backgroundColor = UIColor.lightGray
}
cell.layoutSubviews()
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.item)!")
let strData = arrData[indexPath.item]
if arrSelectedIndex.contains(indexPath) {
arrSelectedIndex = arrSelectedIndex.filter { $0 != indexPath}
arrSelectedData = arrSelectedData.filter { $0 != strData}
}
else {
arrSelectedIndex.append(indexPath)
arrSelectedData.append(strData)
}
collectionView.reloadData()
}
}
You can write the code like this to Enable Multiple Selection :-
yourCollectionViewName.allowsMultipleSelection = true
then you can Do it like this to see the cell Selected -
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var cell = collectionView.cellForItemAtIndexPath(indexPath)
if cell?.selected == true {
cell?.backgroundColor = UIColor.orangeColor()
}
}
To Deselect You can do something Like this -
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
var cell = collectionView.cellForItemAtIndexPath(indexPath)
cell?.backgroundColor = UIColor.clearColor()
}
Enable Multiple Selection
collectionView.allowsMultipleSelection = true
Overrider isSelected property of collectionViewCell.
override var isSelected: Bool {
didSet {
if self.isSelected {
//You can change this method according to your need.
setSelected()
}
else {
//You can change this method according to your need.
setUnselected()
}
}
}
func setSelected(){
bgView.layer.borderWidth = 4
bgView.layer.borderColor = UIColor.Palette.darkBlue.cgColor
bgView.backgroundColor = .blue.withAlphaComponent(0.2)
}
func setUnselected(){
bgView.layer.borderWidth = 0
bgView.backgroundColor = .white
}
You can print selected cell's indexPath
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(collectionView.indexPathsForSelectedItems)
}
Well, to achieve a thing like that, you need to mainly perform the following tasks
Whenever user clicks on a particular cell, you need to change the background colour for that item in the didSelectItemAt delegate method of UICollectionView
Now to send that data to server, you need an array to store all the selected cells and then send that array to server . You can perform the same in didSelectItemAt method as well
I can show you a prototype of what the function will look like:
Let's assume you have an array named arrayForPopulating for populating data inside Collection View and we have array named finalSelections which consist of names of all the selections that user made
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
// Change the background colour of the cell here
cell.contentView.backgroundColor = UIColor.red
// Add the selected cell's data to the array
finalSelections.append(arrayForPopulating[indexPath.row])
}
Now you can send you finalSelections array to the server !

TableView Items get refresh on scrolling

Having UICollectionView as a child of UITableView row. UICollectionView contains images, but whenever I scroll tableview down and up the collection view images got vanished randomly. I am attaching images for my problem reference. Please suggest me how to stop this.
I want my tableview to be like this. And its items should not change on scrolling.
----------------------------------------------------------------------------------------------------------------------------------------------
The collectionview images got vanish on scrolling tableview. It looks like this after scrolling up.
Code Is as follow:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell:PartOfLookTableViewCell = self.looksListTable.dequeueReusableCell(withIdentifier: "cell") as! PartOfLookTableViewCell
let oneRecord = looksArray[indexPath.row]
cell.myCollectionView.loadInitial(_dataArray: oneRecord.imagesArray, isLooks: 1)
return cell
}
Code for loading data to CollectionView:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: looksReuseIdentifier, for: indexPath) as! CustomCollectionViewCell
let oneRecord = inputArray[indexPath.row]
cell.productImage.sd_setImage(with: URL.init(string: oneRecord.thumb_url)){ (image, error, cacheType, url) in
DispatchQueue.main.async {
cell.productImage.image = image
}
}
}
}
}
#Sourabh Bissa :
UITableView reuses the cell using method CellForRowAtIndexPath whenever your new cell gets visible your this method reuse the data source.
The very important thing here is to maintain the data source:
In your case cell for the row at index path giving the updated value to the collection view method but you are not reloading in main Queue. Try to do it immediately after you get the data source.
Your Cell for the row at index path will look like this :
guard let cell = self.tableview.dequeueReusableCell(withIdentifier: "cell") as! PartOfLookTableViewCell else {
return UITableViewCell()
}
let oneRecord = looksArray[indexPath.row]
cell.configureCell(for record : oneRecord with looks : 1)
return cell
and Now in the cell, you will have collection view outlet, where you will implement a collection view data source method and there you download your images asynchronously.
Cell Class will look like this :
class PartOfLookTableViewCell: UITableViewCell {
#IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
collectionView.delegate = self
collectionView.dataSource = self
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func configureCell(for record : Record , with looks : Int) {
// Here reload your collection view
// This collection view will be specific to the cell.
collectionView.reloadData()
}
}
extension PartOfLookTableViewCell : UICollectionViewDelegate , UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//return array
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Asyncronously download images
}
}
This is how you can achieve your requirements without using any tags. Please let me know if you have any Queries in it.

collectionview cell for xib not showing

I am trying to create a custom cell for my collection view. I have a xib named AccountCell:
In my view containing the collection view:
profileViewController = p;
collectionView.dataSource = profileViewController
collectionView.registerClass(SocialAccountCell.self, forCellWithReuseIdentifier: "accountCell")
and then in the data source:
func collectionView(_collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
println("getting cell")
let cell = _collectionView.dequeueReusableCellWithReuseIdentifier("accountCell", forIndexPath: indexPath) as! SocialAccountCell
cell.test()
return cell
}
func collectionView(_collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return user!.accounts.count
}
SocialAccountCell.swift:
class SocialAccountCell: UICollectionViewCell
{
func test()
{
println("Hello From collection cell")
}
}
Both "getting cell" and "Hello From collection cell" are printed but no blue cell appears in the collection view.
I tried adding an image to the cell to check if it was just the blue not working. Still no indication of any cell appearing.
What steps am I missing here?
Since you're using xib to define a cell, I think you'd use collectionView.registerNib(UINib(nibName: "someNib", bundle: nil), forCellWithReuseIdentifier: "accountCell"), or else you'd setup UI elements in code.

Resources