UITableView not check marking cells (outside of didSelect and didDeselect) in Swift
I have a UITableView
where I allow the user to select cells and when they do I set the cell.accessoryType
to .checkmark
. My app is set up in a way that the user is supposed to go to the "main" ViewController, segue to the TableView, select a few cells, and then repeat this process a few times. My problem is that I don't want the tableView selections the user has already made to disappear. I have already tried self.clearsSelectionOnViewWillAppear = false
and it does not work. So I made an array, that contains all of the indexPaths for the cells the user has selected, so when the user goes to the "main" ViewController and comes back I want my app to essentially reselect those cells.
Here is my code for this process...
for row in selectedRows {
print("Reselecting: (row)")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: row)
print(cell)
cell.accessoryType = .checkmark
}
When I print the cell it shows up in the log like this <UITableViewCell: 0x7fc2f90f3e00; frame = (0 0; 375 44); text = 'Title'; clipsToBounds = YES; layer = <CALayer: 0x600000aae260>>
. One thing I noticed is that the text on my cell when it is properly displayed is not Title like the log suggests (Although it is the default text in my storyboard), could this have something to do with my problem? Why are the cells not check marked after running this code?
ios swift uitableview swift4
add a comment |
I have a UITableView
where I allow the user to select cells and when they do I set the cell.accessoryType
to .checkmark
. My app is set up in a way that the user is supposed to go to the "main" ViewController, segue to the TableView, select a few cells, and then repeat this process a few times. My problem is that I don't want the tableView selections the user has already made to disappear. I have already tried self.clearsSelectionOnViewWillAppear = false
and it does not work. So I made an array, that contains all of the indexPaths for the cells the user has selected, so when the user goes to the "main" ViewController and comes back I want my app to essentially reselect those cells.
Here is my code for this process...
for row in selectedRows {
print("Reselecting: (row)")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: row)
print(cell)
cell.accessoryType = .checkmark
}
When I print the cell it shows up in the log like this <UITableViewCell: 0x7fc2f90f3e00; frame = (0 0; 375 44); text = 'Title'; clipsToBounds = YES; layer = <CALayer: 0x600000aae260>>
. One thing I noticed is that the text on my cell when it is properly displayed is not Title like the log suggests (Although it is the default text in my storyboard), could this have something to do with my problem? Why are the cells not check marked after running this code?
ios swift uitableview swift4
2
Store the selection status along with the dataSource model that you are using to populate the cells.
– PGDev
Nov 22 '18 at 16:15
This cannot work. You must not usedequeueReusableCell
outside ofcellForRow
. Add aisSelected
property to your model and maintain the state there. Please see this example
– vadian
Nov 22 '18 at 16:16
@vikingosegundo This doesn't work either if some of the cells are off-screen. Basically manipulating the view directly is a bad choice.
– vadian
Nov 22 '18 at 16:31
@vikingosegundo I have already tried doing that and when I print the cell is shows up in the log as nil.
– coder
Nov 22 '18 at 16:34
add a comment |
I have a UITableView
where I allow the user to select cells and when they do I set the cell.accessoryType
to .checkmark
. My app is set up in a way that the user is supposed to go to the "main" ViewController, segue to the TableView, select a few cells, and then repeat this process a few times. My problem is that I don't want the tableView selections the user has already made to disappear. I have already tried self.clearsSelectionOnViewWillAppear = false
and it does not work. So I made an array, that contains all of the indexPaths for the cells the user has selected, so when the user goes to the "main" ViewController and comes back I want my app to essentially reselect those cells.
Here is my code for this process...
for row in selectedRows {
print("Reselecting: (row)")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: row)
print(cell)
cell.accessoryType = .checkmark
}
When I print the cell it shows up in the log like this <UITableViewCell: 0x7fc2f90f3e00; frame = (0 0; 375 44); text = 'Title'; clipsToBounds = YES; layer = <CALayer: 0x600000aae260>>
. One thing I noticed is that the text on my cell when it is properly displayed is not Title like the log suggests (Although it is the default text in my storyboard), could this have something to do with my problem? Why are the cells not check marked after running this code?
ios swift uitableview swift4
I have a UITableView
where I allow the user to select cells and when they do I set the cell.accessoryType
to .checkmark
. My app is set up in a way that the user is supposed to go to the "main" ViewController, segue to the TableView, select a few cells, and then repeat this process a few times. My problem is that I don't want the tableView selections the user has already made to disappear. I have already tried self.clearsSelectionOnViewWillAppear = false
and it does not work. So I made an array, that contains all of the indexPaths for the cells the user has selected, so when the user goes to the "main" ViewController and comes back I want my app to essentially reselect those cells.
Here is my code for this process...
for row in selectedRows {
print("Reselecting: (row)")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: row)
print(cell)
cell.accessoryType = .checkmark
}
When I print the cell it shows up in the log like this <UITableViewCell: 0x7fc2f90f3e00; frame = (0 0; 375 44); text = 'Title'; clipsToBounds = YES; layer = <CALayer: 0x600000aae260>>
. One thing I noticed is that the text on my cell when it is properly displayed is not Title like the log suggests (Although it is the default text in my storyboard), could this have something to do with my problem? Why are the cells not check marked after running this code?
ios swift uitableview swift4
ios swift uitableview swift4
asked Nov 22 '18 at 16:11
codercoder
867
867
2
Store the selection status along with the dataSource model that you are using to populate the cells.
– PGDev
Nov 22 '18 at 16:15
This cannot work. You must not usedequeueReusableCell
outside ofcellForRow
. Add aisSelected
property to your model and maintain the state there. Please see this example
– vadian
Nov 22 '18 at 16:16
@vikingosegundo This doesn't work either if some of the cells are off-screen. Basically manipulating the view directly is a bad choice.
– vadian
Nov 22 '18 at 16:31
@vikingosegundo I have already tried doing that and when I print the cell is shows up in the log as nil.
– coder
Nov 22 '18 at 16:34
add a comment |
2
Store the selection status along with the dataSource model that you are using to populate the cells.
– PGDev
Nov 22 '18 at 16:15
This cannot work. You must not usedequeueReusableCell
outside ofcellForRow
. Add aisSelected
property to your model and maintain the state there. Please see this example
– vadian
Nov 22 '18 at 16:16
@vikingosegundo This doesn't work either if some of the cells are off-screen. Basically manipulating the view directly is a bad choice.
– vadian
Nov 22 '18 at 16:31
@vikingosegundo I have already tried doing that and when I print the cell is shows up in the log as nil.
– coder
Nov 22 '18 at 16:34
2
2
Store the selection status along with the dataSource model that you are using to populate the cells.
– PGDev
Nov 22 '18 at 16:15
Store the selection status along with the dataSource model that you are using to populate the cells.
– PGDev
Nov 22 '18 at 16:15
This cannot work. You must not use
dequeueReusableCell
outside of cellForRow
. Add a isSelected
property to your model and maintain the state there. Please see this example– vadian
Nov 22 '18 at 16:16
This cannot work. You must not use
dequeueReusableCell
outside of cellForRow
. Add a isSelected
property to your model and maintain the state there. Please see this example– vadian
Nov 22 '18 at 16:16
@vikingosegundo This doesn't work either if some of the cells are off-screen. Basically manipulating the view directly is a bad choice.
– vadian
Nov 22 '18 at 16:31
@vikingosegundo This doesn't work either if some of the cells are off-screen. Basically manipulating the view directly is a bad choice.
– vadian
Nov 22 '18 at 16:31
@vikingosegundo I have already tried doing that and when I print the cell is shows up in the log as nil.
– coder
Nov 22 '18 at 16:34
@vikingosegundo I have already tried doing that and when I print the cell is shows up in the log as nil.
– coder
Nov 22 '18 at 16:34
add a comment |
1 Answer
1
active
oldest
votes
I think you should use tableView(_:cellForItemAt:)
from UITableViewDataSource
protocol.
func tableView(_ tableView: UITableView, cellForItemAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "<your reusable identifier", for: indexPath) as! YourCellSubclassIfAny
cell.accessoryType = isIndexPathSelected(indexPath) ? .checkmark : .none
// do whatever setup you need
return cell
}
Of course you need to set the dataSource
of your tableView
(if not using UITableViewController
. Usually it is set to the view controller which contains the tableView
. You can do it in storyboard or in code.
I tried using this code and it did not seem to work. When is it supposed to be called? I put a print statement inside and never saw it in the log.
– coder
Nov 22 '18 at 16:56
The dataSource is called whenever a cell is displayed on the screen....it is the place where you set the data to the cell
– olejnjak
Nov 22 '18 at 21:17
add a comment |
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%2f53434773%2fuitableview-not-check-marking-cells-outside-of-didselect-and-diddeselect-in-sw%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you should use tableView(_:cellForItemAt:)
from UITableViewDataSource
protocol.
func tableView(_ tableView: UITableView, cellForItemAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "<your reusable identifier", for: indexPath) as! YourCellSubclassIfAny
cell.accessoryType = isIndexPathSelected(indexPath) ? .checkmark : .none
// do whatever setup you need
return cell
}
Of course you need to set the dataSource
of your tableView
(if not using UITableViewController
. Usually it is set to the view controller which contains the tableView
. You can do it in storyboard or in code.
I tried using this code and it did not seem to work. When is it supposed to be called? I put a print statement inside and never saw it in the log.
– coder
Nov 22 '18 at 16:56
The dataSource is called whenever a cell is displayed on the screen....it is the place where you set the data to the cell
– olejnjak
Nov 22 '18 at 21:17
add a comment |
I think you should use tableView(_:cellForItemAt:)
from UITableViewDataSource
protocol.
func tableView(_ tableView: UITableView, cellForItemAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "<your reusable identifier", for: indexPath) as! YourCellSubclassIfAny
cell.accessoryType = isIndexPathSelected(indexPath) ? .checkmark : .none
// do whatever setup you need
return cell
}
Of course you need to set the dataSource
of your tableView
(if not using UITableViewController
. Usually it is set to the view controller which contains the tableView
. You can do it in storyboard or in code.
I tried using this code and it did not seem to work. When is it supposed to be called? I put a print statement inside and never saw it in the log.
– coder
Nov 22 '18 at 16:56
The dataSource is called whenever a cell is displayed on the screen....it is the place where you set the data to the cell
– olejnjak
Nov 22 '18 at 21:17
add a comment |
I think you should use tableView(_:cellForItemAt:)
from UITableViewDataSource
protocol.
func tableView(_ tableView: UITableView, cellForItemAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "<your reusable identifier", for: indexPath) as! YourCellSubclassIfAny
cell.accessoryType = isIndexPathSelected(indexPath) ? .checkmark : .none
// do whatever setup you need
return cell
}
Of course you need to set the dataSource
of your tableView
(if not using UITableViewController
. Usually it is set to the view controller which contains the tableView
. You can do it in storyboard or in code.
I think you should use tableView(_:cellForItemAt:)
from UITableViewDataSource
protocol.
func tableView(_ tableView: UITableView, cellForItemAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "<your reusable identifier", for: indexPath) as! YourCellSubclassIfAny
cell.accessoryType = isIndexPathSelected(indexPath) ? .checkmark : .none
// do whatever setup you need
return cell
}
Of course you need to set the dataSource
of your tableView
(if not using UITableViewController
. Usually it is set to the view controller which contains the tableView
. You can do it in storyboard or in code.
answered Nov 22 '18 at 16:43
olejnjakolejnjak
458312
458312
I tried using this code and it did not seem to work. When is it supposed to be called? I put a print statement inside and never saw it in the log.
– coder
Nov 22 '18 at 16:56
The dataSource is called whenever a cell is displayed on the screen....it is the place where you set the data to the cell
– olejnjak
Nov 22 '18 at 21:17
add a comment |
I tried using this code and it did not seem to work. When is it supposed to be called? I put a print statement inside and never saw it in the log.
– coder
Nov 22 '18 at 16:56
The dataSource is called whenever a cell is displayed on the screen....it is the place where you set the data to the cell
– olejnjak
Nov 22 '18 at 21:17
I tried using this code and it did not seem to work. When is it supposed to be called? I put a print statement inside and never saw it in the log.
– coder
Nov 22 '18 at 16:56
I tried using this code and it did not seem to work. When is it supposed to be called? I put a print statement inside and never saw it in the log.
– coder
Nov 22 '18 at 16:56
The dataSource is called whenever a cell is displayed on the screen....it is the place where you set the data to the cell
– olejnjak
Nov 22 '18 at 21:17
The dataSource is called whenever a cell is displayed on the screen....it is the place where you set the data to the cell
– olejnjak
Nov 22 '18 at 21:17
add a comment |
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%2f53434773%2fuitableview-not-check-marking-cells-outside-of-didselect-and-diddeselect-in-sw%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
2
Store the selection status along with the dataSource model that you are using to populate the cells.
– PGDev
Nov 22 '18 at 16:15
This cannot work. You must not use
dequeueReusableCell
outside ofcellForRow
. Add aisSelected
property to your model and maintain the state there. Please see this example– vadian
Nov 22 '18 at 16:16
@vikingosegundo This doesn't work either if some of the cells are off-screen. Basically manipulating the view directly is a bad choice.
– vadian
Nov 22 '18 at 16:31
@vikingosegundo I have already tried doing that and when I print the cell is shows up in the log as nil.
– coder
Nov 22 '18 at 16:34