How to change particular cell color in ios swift3
I have an String array like [1,2,3,4,5]. I am displaying this array in tableview. My Question is If I want to change the color of 3rd cell only, How can I compare 3rd element with array in ios swift?
ios iphone swift
add a comment |
I have an String array like [1,2,3,4,5]. I am displaying this array in tableview. My Question is If I want to change the color of 3rd cell only, How can I compare 3rd element with array in ios swift?
ios iphone swift
you want to change the 3rd cell or condition matched of 3
– Anbu.Karthik
Nov 16 '18 at 7:55
condition matched of 3
– ios 6457468234
Nov 16 '18 at 7:59
add a comment |
I have an String array like [1,2,3,4,5]. I am displaying this array in tableview. My Question is If I want to change the color of 3rd cell only, How can I compare 3rd element with array in ios swift?
ios iphone swift
I have an String array like [1,2,3,4,5]. I am displaying this array in tableview. My Question is If I want to change the color of 3rd cell only, How can I compare 3rd element with array in ios swift?
ios iphone swift
ios iphone swift
asked Nov 16 '18 at 7:44
ios 6457468234ios 6457468234
446
446
you want to change the 3rd cell or condition matched of 3
– Anbu.Karthik
Nov 16 '18 at 7:55
condition matched of 3
– ios 6457468234
Nov 16 '18 at 7:59
add a comment |
you want to change the 3rd cell or condition matched of 3
– Anbu.Karthik
Nov 16 '18 at 7:55
condition matched of 3
– ios 6457468234
Nov 16 '18 at 7:59
you want to change the 3rd cell or condition matched of 3
– Anbu.Karthik
Nov 16 '18 at 7:55
you want to change the 3rd cell or condition matched of 3
– Anbu.Karthik
Nov 16 '18 at 7:55
condition matched of 3
– ios 6457468234
Nov 16 '18 at 7:59
condition matched of 3
– ios 6457468234
Nov 16 '18 at 7:59
add a comment |
3 Answers
3
active
oldest
votes
on your tableview delegate method based on the condition change the color what you want
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = indexPath.row == 2 ? .red : yellow //or use cell.contentView.backgroundColor = = indexPath.row == 2 ? .red : yellow
}
if you want to change the array contains 3 condition based then use like
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = yourArrayName[indexPath.row] == "3" ? .red : yellow //or use cell.contentView.backgroundColor = = yourArrayName[indexPath.row] == "3" ? .red : yellow
}
okay we go with alternate way
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = .yellow
if let index = yourArrayName.index(of: "3") {
cell.backgroundColor = indexPath.row == index ? .red : yellow
}
}
Its working Fine. I accepted your answer also. Thank you
– ios 6457468234
Nov 16 '18 at 8:07
@ios6457468234 - welcome bro
– Anbu.Karthik
Nov 16 '18 at 8:08
Karthik : I have phone numbers array, I am comparing this array with my device contacts.My Problem is, my array contains only 10 digits for each element but my device contacts saved with +91 also. like 9848022338 == +919848022338. So The above code is not working.
– ios 6457468234
Nov 16 '18 at 8:44
How can I trim +91?
– ios 6457468234
Nov 16 '18 at 8:56
@ios6457468234 - I updated the answer check once bro
– Anbu.Karthik
Nov 16 '18 at 9:04
add a comment |
You can check the condition inside your cellforrow function like this too :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleCellIndentifier", for: indexPath) as! ExampleCell
if indexPath.row == 2{
// Do your modification on the 3rd cell item
cell.backgroundColor = .red
}
return cell
}
you are compare with indexpath.row. But I need to compare with element
– ios 6457468234
Nov 16 '18 at 8:00
maybe this is my mistake for your question because you mentioned that you want to change the color of the 3rd cell only, so next time you should specify more clearer than this
– Anonymous-E
Nov 16 '18 at 8:15
add a comment |
I just applied condition under cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldInTableViewCell") as! myProfieTabelViewCellTableViewCell
if indexPath[1] % 2 == 0{
cell.backgroundColor = #colorLiteral(red: 0.9088078997, green: 0.9088078997, blue: 0.9088078997, alpha: 1)
print(indexPath[1])
}
else{
cell.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
cell.selectionStyle = .none
return 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%2f53333448%2fhow-to-change-particular-cell-color-in-ios-swift3%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
on your tableview delegate method based on the condition change the color what you want
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = indexPath.row == 2 ? .red : yellow //or use cell.contentView.backgroundColor = = indexPath.row == 2 ? .red : yellow
}
if you want to change the array contains 3 condition based then use like
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = yourArrayName[indexPath.row] == "3" ? .red : yellow //or use cell.contentView.backgroundColor = = yourArrayName[indexPath.row] == "3" ? .red : yellow
}
okay we go with alternate way
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = .yellow
if let index = yourArrayName.index(of: "3") {
cell.backgroundColor = indexPath.row == index ? .red : yellow
}
}
Its working Fine. I accepted your answer also. Thank you
– ios 6457468234
Nov 16 '18 at 8:07
@ios6457468234 - welcome bro
– Anbu.Karthik
Nov 16 '18 at 8:08
Karthik : I have phone numbers array, I am comparing this array with my device contacts.My Problem is, my array contains only 10 digits for each element but my device contacts saved with +91 also. like 9848022338 == +919848022338. So The above code is not working.
– ios 6457468234
Nov 16 '18 at 8:44
How can I trim +91?
– ios 6457468234
Nov 16 '18 at 8:56
@ios6457468234 - I updated the answer check once bro
– Anbu.Karthik
Nov 16 '18 at 9:04
add a comment |
on your tableview delegate method based on the condition change the color what you want
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = indexPath.row == 2 ? .red : yellow //or use cell.contentView.backgroundColor = = indexPath.row == 2 ? .red : yellow
}
if you want to change the array contains 3 condition based then use like
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = yourArrayName[indexPath.row] == "3" ? .red : yellow //or use cell.contentView.backgroundColor = = yourArrayName[indexPath.row] == "3" ? .red : yellow
}
okay we go with alternate way
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = .yellow
if let index = yourArrayName.index(of: "3") {
cell.backgroundColor = indexPath.row == index ? .red : yellow
}
}
Its working Fine. I accepted your answer also. Thank you
– ios 6457468234
Nov 16 '18 at 8:07
@ios6457468234 - welcome bro
– Anbu.Karthik
Nov 16 '18 at 8:08
Karthik : I have phone numbers array, I am comparing this array with my device contacts.My Problem is, my array contains only 10 digits for each element but my device contacts saved with +91 also. like 9848022338 == +919848022338. So The above code is not working.
– ios 6457468234
Nov 16 '18 at 8:44
How can I trim +91?
– ios 6457468234
Nov 16 '18 at 8:56
@ios6457468234 - I updated the answer check once bro
– Anbu.Karthik
Nov 16 '18 at 9:04
add a comment |
on your tableview delegate method based on the condition change the color what you want
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = indexPath.row == 2 ? .red : yellow //or use cell.contentView.backgroundColor = = indexPath.row == 2 ? .red : yellow
}
if you want to change the array contains 3 condition based then use like
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = yourArrayName[indexPath.row] == "3" ? .red : yellow //or use cell.contentView.backgroundColor = = yourArrayName[indexPath.row] == "3" ? .red : yellow
}
okay we go with alternate way
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = .yellow
if let index = yourArrayName.index(of: "3") {
cell.backgroundColor = indexPath.row == index ? .red : yellow
}
}
on your tableview delegate method based on the condition change the color what you want
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = indexPath.row == 2 ? .red : yellow //or use cell.contentView.backgroundColor = = indexPath.row == 2 ? .red : yellow
}
if you want to change the array contains 3 condition based then use like
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = yourArrayName[indexPath.row] == "3" ? .red : yellow //or use cell.contentView.backgroundColor = = yourArrayName[indexPath.row] == "3" ? .red : yellow
}
okay we go with alternate way
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = .yellow
if let index = yourArrayName.index(of: "3") {
cell.backgroundColor = indexPath.row == index ? .red : yellow
}
}
edited Nov 16 '18 at 9:04
answered Nov 16 '18 at 7:50
Anbu.KarthikAnbu.Karthik
63.3k17123112
63.3k17123112
Its working Fine. I accepted your answer also. Thank you
– ios 6457468234
Nov 16 '18 at 8:07
@ios6457468234 - welcome bro
– Anbu.Karthik
Nov 16 '18 at 8:08
Karthik : I have phone numbers array, I am comparing this array with my device contacts.My Problem is, my array contains only 10 digits for each element but my device contacts saved with +91 also. like 9848022338 == +919848022338. So The above code is not working.
– ios 6457468234
Nov 16 '18 at 8:44
How can I trim +91?
– ios 6457468234
Nov 16 '18 at 8:56
@ios6457468234 - I updated the answer check once bro
– Anbu.Karthik
Nov 16 '18 at 9:04
add a comment |
Its working Fine. I accepted your answer also. Thank you
– ios 6457468234
Nov 16 '18 at 8:07
@ios6457468234 - welcome bro
– Anbu.Karthik
Nov 16 '18 at 8:08
Karthik : I have phone numbers array, I am comparing this array with my device contacts.My Problem is, my array contains only 10 digits for each element but my device contacts saved with +91 also. like 9848022338 == +919848022338. So The above code is not working.
– ios 6457468234
Nov 16 '18 at 8:44
How can I trim +91?
– ios 6457468234
Nov 16 '18 at 8:56
@ios6457468234 - I updated the answer check once bro
– Anbu.Karthik
Nov 16 '18 at 9:04
Its working Fine. I accepted your answer also. Thank you
– ios 6457468234
Nov 16 '18 at 8:07
Its working Fine. I accepted your answer also. Thank you
– ios 6457468234
Nov 16 '18 at 8:07
@ios6457468234 - welcome bro
– Anbu.Karthik
Nov 16 '18 at 8:08
@ios6457468234 - welcome bro
– Anbu.Karthik
Nov 16 '18 at 8:08
Karthik : I have phone numbers array, I am comparing this array with my device contacts.My Problem is, my array contains only 10 digits for each element but my device contacts saved with +91 also. like 9848022338 == +919848022338. So The above code is not working.
– ios 6457468234
Nov 16 '18 at 8:44
Karthik : I have phone numbers array, I am comparing this array with my device contacts.My Problem is, my array contains only 10 digits for each element but my device contacts saved with +91 also. like 9848022338 == +919848022338. So The above code is not working.
– ios 6457468234
Nov 16 '18 at 8:44
How can I trim +91?
– ios 6457468234
Nov 16 '18 at 8:56
How can I trim +91?
– ios 6457468234
Nov 16 '18 at 8:56
@ios6457468234 - I updated the answer check once bro
– Anbu.Karthik
Nov 16 '18 at 9:04
@ios6457468234 - I updated the answer check once bro
– Anbu.Karthik
Nov 16 '18 at 9:04
add a comment |
You can check the condition inside your cellforrow function like this too :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleCellIndentifier", for: indexPath) as! ExampleCell
if indexPath.row == 2{
// Do your modification on the 3rd cell item
cell.backgroundColor = .red
}
return cell
}
you are compare with indexpath.row. But I need to compare with element
– ios 6457468234
Nov 16 '18 at 8:00
maybe this is my mistake for your question because you mentioned that you want to change the color of the 3rd cell only, so next time you should specify more clearer than this
– Anonymous-E
Nov 16 '18 at 8:15
add a comment |
You can check the condition inside your cellforrow function like this too :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleCellIndentifier", for: indexPath) as! ExampleCell
if indexPath.row == 2{
// Do your modification on the 3rd cell item
cell.backgroundColor = .red
}
return cell
}
you are compare with indexpath.row. But I need to compare with element
– ios 6457468234
Nov 16 '18 at 8:00
maybe this is my mistake for your question because you mentioned that you want to change the color of the 3rd cell only, so next time you should specify more clearer than this
– Anonymous-E
Nov 16 '18 at 8:15
add a comment |
You can check the condition inside your cellforrow function like this too :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleCellIndentifier", for: indexPath) as! ExampleCell
if indexPath.row == 2{
// Do your modification on the 3rd cell item
cell.backgroundColor = .red
}
return cell
}
You can check the condition inside your cellforrow function like this too :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleCellIndentifier", for: indexPath) as! ExampleCell
if indexPath.row == 2{
// Do your modification on the 3rd cell item
cell.backgroundColor = .red
}
return cell
}
answered Nov 16 '18 at 7:57
Anonymous-EAnonymous-E
528318
528318
you are compare with indexpath.row. But I need to compare with element
– ios 6457468234
Nov 16 '18 at 8:00
maybe this is my mistake for your question because you mentioned that you want to change the color of the 3rd cell only, so next time you should specify more clearer than this
– Anonymous-E
Nov 16 '18 at 8:15
add a comment |
you are compare with indexpath.row. But I need to compare with element
– ios 6457468234
Nov 16 '18 at 8:00
maybe this is my mistake for your question because you mentioned that you want to change the color of the 3rd cell only, so next time you should specify more clearer than this
– Anonymous-E
Nov 16 '18 at 8:15
you are compare with indexpath.row. But I need to compare with element
– ios 6457468234
Nov 16 '18 at 8:00
you are compare with indexpath.row. But I need to compare with element
– ios 6457468234
Nov 16 '18 at 8:00
maybe this is my mistake for your question because you mentioned that you want to change the color of the 3rd cell only, so next time you should specify more clearer than this
– Anonymous-E
Nov 16 '18 at 8:15
maybe this is my mistake for your question because you mentioned that you want to change the color of the 3rd cell only, so next time you should specify more clearer than this
– Anonymous-E
Nov 16 '18 at 8:15
add a comment |
I just applied condition under cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldInTableViewCell") as! myProfieTabelViewCellTableViewCell
if indexPath[1] % 2 == 0{
cell.backgroundColor = #colorLiteral(red: 0.9088078997, green: 0.9088078997, blue: 0.9088078997, alpha: 1)
print(indexPath[1])
}
else{
cell.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
cell.selectionStyle = .none
return cell
}
add a comment |
I just applied condition under cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldInTableViewCell") as! myProfieTabelViewCellTableViewCell
if indexPath[1] % 2 == 0{
cell.backgroundColor = #colorLiteral(red: 0.9088078997, green: 0.9088078997, blue: 0.9088078997, alpha: 1)
print(indexPath[1])
}
else{
cell.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
cell.selectionStyle = .none
return cell
}
add a comment |
I just applied condition under cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldInTableViewCell") as! myProfieTabelViewCellTableViewCell
if indexPath[1] % 2 == 0{
cell.backgroundColor = #colorLiteral(red: 0.9088078997, green: 0.9088078997, blue: 0.9088078997, alpha: 1)
print(indexPath[1])
}
else{
cell.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
cell.selectionStyle = .none
return cell
}
I just applied condition under cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldInTableViewCell") as! myProfieTabelViewCellTableViewCell
if indexPath[1] % 2 == 0{
cell.backgroundColor = #colorLiteral(red: 0.9088078997, green: 0.9088078997, blue: 0.9088078997, alpha: 1)
print(indexPath[1])
}
else{
cell.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
cell.selectionStyle = .none
return cell
}
answered Nov 16 '18 at 9:53
Haani DevHaani Dev
5217
5217
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%2f53333448%2fhow-to-change-particular-cell-color-in-ios-swift3%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
you want to change the 3rd cell or condition matched of 3
– Anbu.Karthik
Nov 16 '18 at 7:55
condition matched of 3
– ios 6457468234
Nov 16 '18 at 7:59