Swift looping through array
I have a UITableView
that has 100 cells. I want to create an array that will hold index values of that table that I want to disable the table cells if the table row selected matches any of the values in the array.
I have found that the following code works to disable a specific cell that I give it.
UITableViewCellSelectionStyle.none
Any help on this would be greatly appreciated.
This is how I am checking which cell is selected:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedRow = tableView.indexPathForSelectedRow?.row
let workoutSelected = selectedRow
stringPassedTableView = workoutSelected!
let myVC = storyboard?.instantiateViewController(withIdentifier: "showWorkout") as! WorkoutViewController
myVC.stringPassed = stringPassedTableView
navigationController?.pushViewController(myVC, animated: true)
}
ios swift loops uitableview
|
show 5 more comments
I have a UITableView
that has 100 cells. I want to create an array that will hold index values of that table that I want to disable the table cells if the table row selected matches any of the values in the array.
I have found that the following code works to disable a specific cell that I give it.
UITableViewCellSelectionStyle.none
Any help on this would be greatly appreciated.
This is how I am checking which cell is selected:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedRow = tableView.indexPathForSelectedRow?.row
let workoutSelected = selectedRow
stringPassedTableView = workoutSelected!
let myVC = storyboard?.instantiateViewController(withIdentifier: "showWorkout") as! WorkoutViewController
myVC.stringPassed = stringPassedTableView
navigationController?.pushViewController(myVC, animated: true)
}
ios swift loops uitableview
I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }
– ZacharyGeorge
Nov 23 '18 at 21:24
So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?
– Joakim Danielson
Nov 23 '18 at 21:25
Joakim - correct
– ZacharyGeorge
Nov 23 '18 at 21:26
Don't use an array containing index paths. Add theselected
orenabled
information to your data model. The benefit is that the Ui will be reliably updated incellForRow
.
– vadian
Nov 23 '18 at 21:26
1
Why do you callindexPathForSelectedRow
? That's pointless. The method gives you the index path in theindexPath
parameter.
– vadian
Nov 23 '18 at 21:38
|
show 5 more comments
I have a UITableView
that has 100 cells. I want to create an array that will hold index values of that table that I want to disable the table cells if the table row selected matches any of the values in the array.
I have found that the following code works to disable a specific cell that I give it.
UITableViewCellSelectionStyle.none
Any help on this would be greatly appreciated.
This is how I am checking which cell is selected:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedRow = tableView.indexPathForSelectedRow?.row
let workoutSelected = selectedRow
stringPassedTableView = workoutSelected!
let myVC = storyboard?.instantiateViewController(withIdentifier: "showWorkout") as! WorkoutViewController
myVC.stringPassed = stringPassedTableView
navigationController?.pushViewController(myVC, animated: true)
}
ios swift loops uitableview
I have a UITableView
that has 100 cells. I want to create an array that will hold index values of that table that I want to disable the table cells if the table row selected matches any of the values in the array.
I have found that the following code works to disable a specific cell that I give it.
UITableViewCellSelectionStyle.none
Any help on this would be greatly appreciated.
This is how I am checking which cell is selected:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedRow = tableView.indexPathForSelectedRow?.row
let workoutSelected = selectedRow
stringPassedTableView = workoutSelected!
let myVC = storyboard?.instantiateViewController(withIdentifier: "showWorkout") as! WorkoutViewController
myVC.stringPassed = stringPassedTableView
navigationController?.pushViewController(myVC, animated: true)
}
ios swift loops uitableview
ios swift loops uitableview
edited Nov 23 '18 at 21:35
ZacharyGeorge
asked Nov 23 '18 at 21:22
ZacharyGeorgeZacharyGeorge
32
32
I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }
– ZacharyGeorge
Nov 23 '18 at 21:24
So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?
– Joakim Danielson
Nov 23 '18 at 21:25
Joakim - correct
– ZacharyGeorge
Nov 23 '18 at 21:26
Don't use an array containing index paths. Add theselected
orenabled
information to your data model. The benefit is that the Ui will be reliably updated incellForRow
.
– vadian
Nov 23 '18 at 21:26
1
Why do you callindexPathForSelectedRow
? That's pointless. The method gives you the index path in theindexPath
parameter.
– vadian
Nov 23 '18 at 21:38
|
show 5 more comments
I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }
– ZacharyGeorge
Nov 23 '18 at 21:24
So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?
– Joakim Danielson
Nov 23 '18 at 21:25
Joakim - correct
– ZacharyGeorge
Nov 23 '18 at 21:26
Don't use an array containing index paths. Add theselected
orenabled
information to your data model. The benefit is that the Ui will be reliably updated incellForRow
.
– vadian
Nov 23 '18 at 21:26
1
Why do you callindexPathForSelectedRow
? That's pointless. The method gives you the index path in theindexPath
parameter.
– vadian
Nov 23 '18 at 21:38
I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }
– ZacharyGeorge
Nov 23 '18 at 21:24
I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }
– ZacharyGeorge
Nov 23 '18 at 21:24
So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?
– Joakim Danielson
Nov 23 '18 at 21:25
So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?
– Joakim Danielson
Nov 23 '18 at 21:25
Joakim - correct
– ZacharyGeorge
Nov 23 '18 at 21:26
Joakim - correct
– ZacharyGeorge
Nov 23 '18 at 21:26
Don't use an array containing index paths. Add the
selected
or enabled
information to your data model. The benefit is that the Ui will be reliably updated in cellForRow
.– vadian
Nov 23 '18 at 21:26
Don't use an array containing index paths. Add the
selected
or enabled
information to your data model. The benefit is that the Ui will be reliably updated in cellForRow
.– vadian
Nov 23 '18 at 21:26
1
1
Why do you call
indexPathForSelectedRow
? That's pointless. The method gives you the index path in the indexPath
parameter.– vadian
Nov 23 '18 at 21:38
Why do you call
indexPathForSelectedRow
? That's pointless. The method gives you the index path in the indexPath
parameter.– vadian
Nov 23 '18 at 21:38
|
show 5 more comments
3 Answers
3
active
oldest
votes
You can use the following tableview delegate method to allow selection or not
tableView:willSelectRowAtIndexPath:
Simply check if the cell index is in your disabled cell indexes and if so return nil. Otherwise return the indexpath.
You can see the documentation here:
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614943-tableview?language=objc#return-value
add a comment |
A much better way that, create an array of your model class which data you are passing into tableView data source.
Add a property into model class like named as "isSelectable" and assign it properly when you are parsing your model class data.
Use this "isSelectable" property, when you will select a row then check this property it is true or false and performs operation accordingly.
Hope you understand !!
add a comment |
You have an array (or set) as a class property
var disabledRows = [IndexPaath]()
then simply use it at the start of the func
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if disabledRows.contains(indexPath) { return }
// code to handle row with enabled cell
}
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%2f53453061%2fswift-looping-through-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the following tableview delegate method to allow selection or not
tableView:willSelectRowAtIndexPath:
Simply check if the cell index is in your disabled cell indexes and if so return nil. Otherwise return the indexpath.
You can see the documentation here:
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614943-tableview?language=objc#return-value
add a comment |
You can use the following tableview delegate method to allow selection or not
tableView:willSelectRowAtIndexPath:
Simply check if the cell index is in your disabled cell indexes and if so return nil. Otherwise return the indexpath.
You can see the documentation here:
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614943-tableview?language=objc#return-value
add a comment |
You can use the following tableview delegate method to allow selection or not
tableView:willSelectRowAtIndexPath:
Simply check if the cell index is in your disabled cell indexes and if so return nil. Otherwise return the indexpath.
You can see the documentation here:
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614943-tableview?language=objc#return-value
You can use the following tableview delegate method to allow selection or not
tableView:willSelectRowAtIndexPath:
Simply check if the cell index is in your disabled cell indexes and if so return nil. Otherwise return the indexpath.
You can see the documentation here:
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614943-tableview?language=objc#return-value
answered Nov 24 '18 at 1:48
jmsjms
33718
33718
add a comment |
add a comment |
A much better way that, create an array of your model class which data you are passing into tableView data source.
Add a property into model class like named as "isSelectable" and assign it properly when you are parsing your model class data.
Use this "isSelectable" property, when you will select a row then check this property it is true or false and performs operation accordingly.
Hope you understand !!
add a comment |
A much better way that, create an array of your model class which data you are passing into tableView data source.
Add a property into model class like named as "isSelectable" and assign it properly when you are parsing your model class data.
Use this "isSelectable" property, when you will select a row then check this property it is true or false and performs operation accordingly.
Hope you understand !!
add a comment |
A much better way that, create an array of your model class which data you are passing into tableView data source.
Add a property into model class like named as "isSelectable" and assign it properly when you are parsing your model class data.
Use this "isSelectable" property, when you will select a row then check this property it is true or false and performs operation accordingly.
Hope you understand !!
A much better way that, create an array of your model class which data you are passing into tableView data source.
Add a property into model class like named as "isSelectable" and assign it properly when you are parsing your model class data.
Use this "isSelectable" property, when you will select a row then check this property it is true or false and performs operation accordingly.
Hope you understand !!
answered Nov 24 '18 at 6:05
iDev750iDev750
5961316
5961316
add a comment |
add a comment |
You have an array (or set) as a class property
var disabledRows = [IndexPaath]()
then simply use it at the start of the func
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if disabledRows.contains(indexPath) { return }
// code to handle row with enabled cell
}
add a comment |
You have an array (or set) as a class property
var disabledRows = [IndexPaath]()
then simply use it at the start of the func
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if disabledRows.contains(indexPath) { return }
// code to handle row with enabled cell
}
add a comment |
You have an array (or set) as a class property
var disabledRows = [IndexPaath]()
then simply use it at the start of the func
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if disabledRows.contains(indexPath) { return }
// code to handle row with enabled cell
}
You have an array (or set) as a class property
var disabledRows = [IndexPaath]()
then simply use it at the start of the func
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if disabledRows.contains(indexPath) { return }
// code to handle row with enabled cell
}
answered Nov 24 '18 at 13:39
Joakim DanielsonJoakim Danielson
9,0553724
9,0553724
add a comment |
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%2f53453061%2fswift-looping-through-array%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
I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }
– ZacharyGeorge
Nov 23 '18 at 21:24
So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?
– Joakim Danielson
Nov 23 '18 at 21:25
Joakim - correct
– ZacharyGeorge
Nov 23 '18 at 21:26
Don't use an array containing index paths. Add the
selected
orenabled
information to your data model. The benefit is that the Ui will be reliably updated incellForRow
.– vadian
Nov 23 '18 at 21:26
1
Why do you call
indexPathForSelectedRow
? That's pointless. The method gives you the index path in theindexPath
parameter.– vadian
Nov 23 '18 at 21:38