UICollectionViewCell executing statements if isSelected is true when it never is
I have a shop as a UICollectionView. I have a system of highlighting the current ball the user will use. There cells can be owned or not owned. When I click on a not owned, if the user does not have enough money to buy it, nothing should happen, and if they do, isSelected should be set to true. The problem is when I click on a locked item, it still gets highlighted, but none of the breakpoints trigger inside the if statement to test if it is bought. I have a didSet on isSelected to highlight the cell.
This is my delegate class
This is a shortened version of the didSelectItemAt function:
//I have an array of structs, called balls, to hold values of each item.
indx = indexPath.item//Universal variable that can be accessed in both this class and the cell class
let cell = collectionView.cellForItem(at: indexPath) as! Shop1CollectionViewCell
balls[indexPath.item].owned = cell.clickedOn(price: balls[indexPath.item].price, index: indexPath.item)
//saves the value to know if you own that item and can select it whenever if you buy it it returns true if not, false
if balls[indexPath.item].owned == true && cell.owned == true{
//This will only pass if the item has been bought, so you can not select items that you don't own
if ogCell.isSelected{
ogCell.isSelected = false
//deselects the saved cell, last selected from last time, that is saved from userdefaults
}
currentItem = indexPath.item//reference for the current item that is selected
cell.isSelected = true
//this will trigger the highlight() function in didSet
lastCell = cell//reference to last cell selected
lastIndex = indexPath.item//index of that cell
}
This is some of my cell class:
//This is what is executed when the isSelected is changed.
override var isSelected: Bool{
didSet{
if self.isSelected{
highlight()
}else{
unHighlight()
}
}
}
//This is what is executed when the cell is clicked on:
if money >= price && !owned{
money -= price
owned = true
isSelected = true
}else if owned == false{
owned = false
isSelected = false
currentItem = lastIndex
lastCell.highlight()
}
When I click on the cell, I have created a way to deselect the selected cell if it is not owned, but this process deselects the previous selection, so I am left with nothing selected. I would like to know why the unowned cell is being highlighted, even when debugging says it never goes through any point that isSelected is set to true.
I can add more code if needed but all other code seemed unnecessary.
ios swift uicollectionview uicollectionviewcell
|
show 2 more comments
I have a shop as a UICollectionView. I have a system of highlighting the current ball the user will use. There cells can be owned or not owned. When I click on a not owned, if the user does not have enough money to buy it, nothing should happen, and if they do, isSelected should be set to true. The problem is when I click on a locked item, it still gets highlighted, but none of the breakpoints trigger inside the if statement to test if it is bought. I have a didSet on isSelected to highlight the cell.
This is my delegate class
This is a shortened version of the didSelectItemAt function:
//I have an array of structs, called balls, to hold values of each item.
indx = indexPath.item//Universal variable that can be accessed in both this class and the cell class
let cell = collectionView.cellForItem(at: indexPath) as! Shop1CollectionViewCell
balls[indexPath.item].owned = cell.clickedOn(price: balls[indexPath.item].price, index: indexPath.item)
//saves the value to know if you own that item and can select it whenever if you buy it it returns true if not, false
if balls[indexPath.item].owned == true && cell.owned == true{
//This will only pass if the item has been bought, so you can not select items that you don't own
if ogCell.isSelected{
ogCell.isSelected = false
//deselects the saved cell, last selected from last time, that is saved from userdefaults
}
currentItem = indexPath.item//reference for the current item that is selected
cell.isSelected = true
//this will trigger the highlight() function in didSet
lastCell = cell//reference to last cell selected
lastIndex = indexPath.item//index of that cell
}
This is some of my cell class:
//This is what is executed when the isSelected is changed.
override var isSelected: Bool{
didSet{
if self.isSelected{
highlight()
}else{
unHighlight()
}
}
}
//This is what is executed when the cell is clicked on:
if money >= price && !owned{
money -= price
owned = true
isSelected = true
}else if owned == false{
owned = false
isSelected = false
currentItem = lastIndex
lastCell.highlight()
}
When I click on the cell, I have created a way to deselect the selected cell if it is not owned, but this process deselects the previous selection, so I am left with nothing selected. I would like to know why the unowned cell is being highlighted, even when debugging says it never goes through any point that isSelected is set to true.
I can add more code if needed but all other code seemed unnecessary.
ios swift uicollectionview uicollectionviewcell
1
Selection involves highlighting. Tapping a cell highlights it. Basically you should not be trying to highlight or select individual cells; let the collection view manage that.
– matt
Nov 21 '18 at 22:29
Is there a way to turn that on or off, because when I started, it never highlighted cells. I would need to customize it because I only want it to highlight owned items. Also, would there be a way to select programmatically?
– Repardeimaj
Nov 22 '18 at 0:07
It seems to me that you're misusing the built-in notion of highlighting to stand in for some sort of customizable state you want your cells to adopt. But a cell cannot hold state because it is view, and a reused view at that. Keep track of state in your model, communicate it thru the data source, implement it visually through the data source or the cell subclass.
– matt
Nov 22 '18 at 0:40
Just to give an example, your q. says "There cells can be owned or not owned". You might just be abbreviating, but if you really mean that, you have a gross misconception. A cell cannot be anything. The model for that index path is what can be owned or not owned. The cell that temporarily slots into that index path is merely a temporary visual reflection of that.
– matt
Nov 22 '18 at 1:27
Yes, I understand what you are saying. I guess I meant each item can be owned or not owned, which is represented by one of the cells.
– Repardeimaj
Nov 22 '18 at 1:36
|
show 2 more comments
I have a shop as a UICollectionView. I have a system of highlighting the current ball the user will use. There cells can be owned or not owned. When I click on a not owned, if the user does not have enough money to buy it, nothing should happen, and if they do, isSelected should be set to true. The problem is when I click on a locked item, it still gets highlighted, but none of the breakpoints trigger inside the if statement to test if it is bought. I have a didSet on isSelected to highlight the cell.
This is my delegate class
This is a shortened version of the didSelectItemAt function:
//I have an array of structs, called balls, to hold values of each item.
indx = indexPath.item//Universal variable that can be accessed in both this class and the cell class
let cell = collectionView.cellForItem(at: indexPath) as! Shop1CollectionViewCell
balls[indexPath.item].owned = cell.clickedOn(price: balls[indexPath.item].price, index: indexPath.item)
//saves the value to know if you own that item and can select it whenever if you buy it it returns true if not, false
if balls[indexPath.item].owned == true && cell.owned == true{
//This will only pass if the item has been bought, so you can not select items that you don't own
if ogCell.isSelected{
ogCell.isSelected = false
//deselects the saved cell, last selected from last time, that is saved from userdefaults
}
currentItem = indexPath.item//reference for the current item that is selected
cell.isSelected = true
//this will trigger the highlight() function in didSet
lastCell = cell//reference to last cell selected
lastIndex = indexPath.item//index of that cell
}
This is some of my cell class:
//This is what is executed when the isSelected is changed.
override var isSelected: Bool{
didSet{
if self.isSelected{
highlight()
}else{
unHighlight()
}
}
}
//This is what is executed when the cell is clicked on:
if money >= price && !owned{
money -= price
owned = true
isSelected = true
}else if owned == false{
owned = false
isSelected = false
currentItem = lastIndex
lastCell.highlight()
}
When I click on the cell, I have created a way to deselect the selected cell if it is not owned, but this process deselects the previous selection, so I am left with nothing selected. I would like to know why the unowned cell is being highlighted, even when debugging says it never goes through any point that isSelected is set to true.
I can add more code if needed but all other code seemed unnecessary.
ios swift uicollectionview uicollectionviewcell
I have a shop as a UICollectionView. I have a system of highlighting the current ball the user will use. There cells can be owned or not owned. When I click on a not owned, if the user does not have enough money to buy it, nothing should happen, and if they do, isSelected should be set to true. The problem is when I click on a locked item, it still gets highlighted, but none of the breakpoints trigger inside the if statement to test if it is bought. I have a didSet on isSelected to highlight the cell.
This is my delegate class
This is a shortened version of the didSelectItemAt function:
//I have an array of structs, called balls, to hold values of each item.
indx = indexPath.item//Universal variable that can be accessed in both this class and the cell class
let cell = collectionView.cellForItem(at: indexPath) as! Shop1CollectionViewCell
balls[indexPath.item].owned = cell.clickedOn(price: balls[indexPath.item].price, index: indexPath.item)
//saves the value to know if you own that item and can select it whenever if you buy it it returns true if not, false
if balls[indexPath.item].owned == true && cell.owned == true{
//This will only pass if the item has been bought, so you can not select items that you don't own
if ogCell.isSelected{
ogCell.isSelected = false
//deselects the saved cell, last selected from last time, that is saved from userdefaults
}
currentItem = indexPath.item//reference for the current item that is selected
cell.isSelected = true
//this will trigger the highlight() function in didSet
lastCell = cell//reference to last cell selected
lastIndex = indexPath.item//index of that cell
}
This is some of my cell class:
//This is what is executed when the isSelected is changed.
override var isSelected: Bool{
didSet{
if self.isSelected{
highlight()
}else{
unHighlight()
}
}
}
//This is what is executed when the cell is clicked on:
if money >= price && !owned{
money -= price
owned = true
isSelected = true
}else if owned == false{
owned = false
isSelected = false
currentItem = lastIndex
lastCell.highlight()
}
When I click on the cell, I have created a way to deselect the selected cell if it is not owned, but this process deselects the previous selection, so I am left with nothing selected. I would like to know why the unowned cell is being highlighted, even when debugging says it never goes through any point that isSelected is set to true.
I can add more code if needed but all other code seemed unnecessary.
ios swift uicollectionview uicollectionviewcell
ios swift uicollectionview uicollectionviewcell
asked Nov 21 '18 at 22:24
RepardeimajRepardeimaj
15112
15112
1
Selection involves highlighting. Tapping a cell highlights it. Basically you should not be trying to highlight or select individual cells; let the collection view manage that.
– matt
Nov 21 '18 at 22:29
Is there a way to turn that on or off, because when I started, it never highlighted cells. I would need to customize it because I only want it to highlight owned items. Also, would there be a way to select programmatically?
– Repardeimaj
Nov 22 '18 at 0:07
It seems to me that you're misusing the built-in notion of highlighting to stand in for some sort of customizable state you want your cells to adopt. But a cell cannot hold state because it is view, and a reused view at that. Keep track of state in your model, communicate it thru the data source, implement it visually through the data source or the cell subclass.
– matt
Nov 22 '18 at 0:40
Just to give an example, your q. says "There cells can be owned or not owned". You might just be abbreviating, but if you really mean that, you have a gross misconception. A cell cannot be anything. The model for that index path is what can be owned or not owned. The cell that temporarily slots into that index path is merely a temporary visual reflection of that.
– matt
Nov 22 '18 at 1:27
Yes, I understand what you are saying. I guess I meant each item can be owned or not owned, which is represented by one of the cells.
– Repardeimaj
Nov 22 '18 at 1:36
|
show 2 more comments
1
Selection involves highlighting. Tapping a cell highlights it. Basically you should not be trying to highlight or select individual cells; let the collection view manage that.
– matt
Nov 21 '18 at 22:29
Is there a way to turn that on or off, because when I started, it never highlighted cells. I would need to customize it because I only want it to highlight owned items. Also, would there be a way to select programmatically?
– Repardeimaj
Nov 22 '18 at 0:07
It seems to me that you're misusing the built-in notion of highlighting to stand in for some sort of customizable state you want your cells to adopt. But a cell cannot hold state because it is view, and a reused view at that. Keep track of state in your model, communicate it thru the data source, implement it visually through the data source or the cell subclass.
– matt
Nov 22 '18 at 0:40
Just to give an example, your q. says "There cells can be owned or not owned". You might just be abbreviating, but if you really mean that, you have a gross misconception. A cell cannot be anything. The model for that index path is what can be owned or not owned. The cell that temporarily slots into that index path is merely a temporary visual reflection of that.
– matt
Nov 22 '18 at 1:27
Yes, I understand what you are saying. I guess I meant each item can be owned or not owned, which is represented by one of the cells.
– Repardeimaj
Nov 22 '18 at 1:36
1
1
Selection involves highlighting. Tapping a cell highlights it. Basically you should not be trying to highlight or select individual cells; let the collection view manage that.
– matt
Nov 21 '18 at 22:29
Selection involves highlighting. Tapping a cell highlights it. Basically you should not be trying to highlight or select individual cells; let the collection view manage that.
– matt
Nov 21 '18 at 22:29
Is there a way to turn that on or off, because when I started, it never highlighted cells. I would need to customize it because I only want it to highlight owned items. Also, would there be a way to select programmatically?
– Repardeimaj
Nov 22 '18 at 0:07
Is there a way to turn that on or off, because when I started, it never highlighted cells. I would need to customize it because I only want it to highlight owned items. Also, would there be a way to select programmatically?
– Repardeimaj
Nov 22 '18 at 0:07
It seems to me that you're misusing the built-in notion of highlighting to stand in for some sort of customizable state you want your cells to adopt. But a cell cannot hold state because it is view, and a reused view at that. Keep track of state in your model, communicate it thru the data source, implement it visually through the data source or the cell subclass.
– matt
Nov 22 '18 at 0:40
It seems to me that you're misusing the built-in notion of highlighting to stand in for some sort of customizable state you want your cells to adopt. But a cell cannot hold state because it is view, and a reused view at that. Keep track of state in your model, communicate it thru the data source, implement it visually through the data source or the cell subclass.
– matt
Nov 22 '18 at 0:40
Just to give an example, your q. says "There cells can be owned or not owned". You might just be abbreviating, but if you really mean that, you have a gross misconception. A cell cannot be anything. The model for that index path is what can be owned or not owned. The cell that temporarily slots into that index path is merely a temporary visual reflection of that.
– matt
Nov 22 '18 at 1:27
Just to give an example, your q. says "There cells can be owned or not owned". You might just be abbreviating, but if you really mean that, you have a gross misconception. A cell cannot be anything. The model for that index path is what can be owned or not owned. The cell that temporarily slots into that index path is merely a temporary visual reflection of that.
– matt
Nov 22 '18 at 1:27
Yes, I understand what you are saying. I guess I meant each item can be owned or not owned, which is represented by one of the cells.
– Repardeimaj
Nov 22 '18 at 1:36
Yes, I understand what you are saying. I guess I meant each item can be owned or not owned, which is represented by one of the cells.
– Repardeimaj
Nov 22 '18 at 1:36
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421286%2fuicollectionviewcell-executing-statements-if-isselected-is-true-when-it-never-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421286%2fuicollectionviewcell-executing-statements-if-isselected-is-true-when-it-never-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Selection involves highlighting. Tapping a cell highlights it. Basically you should not be trying to highlight or select individual cells; let the collection view manage that.
– matt
Nov 21 '18 at 22:29
Is there a way to turn that on or off, because when I started, it never highlighted cells. I would need to customize it because I only want it to highlight owned items. Also, would there be a way to select programmatically?
– Repardeimaj
Nov 22 '18 at 0:07
It seems to me that you're misusing the built-in notion of highlighting to stand in for some sort of customizable state you want your cells to adopt. But a cell cannot hold state because it is view, and a reused view at that. Keep track of state in your model, communicate it thru the data source, implement it visually through the data source or the cell subclass.
– matt
Nov 22 '18 at 0:40
Just to give an example, your q. says "There cells can be owned or not owned". You might just be abbreviating, but if you really mean that, you have a gross misconception. A cell cannot be anything. The model for that index path is what can be owned or not owned. The cell that temporarily slots into that index path is merely a temporary visual reflection of that.
– matt
Nov 22 '18 at 1:27
Yes, I understand what you are saying. I guess I meant each item can be owned or not owned, which is represented by one of the cells.
– Repardeimaj
Nov 22 '18 at 1:36