Customise static tableview Header - Swift
I am trying to customize static UITableViewController section header with UITableViewCell.
I could successfully cusomise section header in dynamic tableView by using the below set of code,
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
headerCell?.textLabel?.text = "Section (section + 1)"
headerCell?.textLabel?.textColor = UIColor.blue
return headerCell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
But, couldn't work for static tableview.
How to customise section header with static tableView
ios uitableview header
add a comment |
I am trying to customize static UITableViewController section header with UITableViewCell.
I could successfully cusomise section header in dynamic tableView by using the below set of code,
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
headerCell?.textLabel?.text = "Section (section + 1)"
headerCell?.textLabel?.textColor = UIColor.blue
return headerCell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
But, couldn't work for static tableview.
How to customise section header with static tableView
ios uitableview header
Could you please clarify what do you mean by static tableView?
– Gkolunia
Nov 22 '18 at 9:10
@Gkolunia: There are to type of tableview static cells and dynamic cells. I am using Static cell for UITableView
– Vineesh TP
Nov 22 '18 at 9:27
add a comment |
I am trying to customize static UITableViewController section header with UITableViewCell.
I could successfully cusomise section header in dynamic tableView by using the below set of code,
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
headerCell?.textLabel?.text = "Section (section + 1)"
headerCell?.textLabel?.textColor = UIColor.blue
return headerCell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
But, couldn't work for static tableview.
How to customise section header with static tableView
ios uitableview header
I am trying to customize static UITableViewController section header with UITableViewCell.
I could successfully cusomise section header in dynamic tableView by using the below set of code,
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
headerCell?.textLabel?.text = "Section (section + 1)"
headerCell?.textLabel?.textColor = UIColor.blue
return headerCell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
But, couldn't work for static tableview.
How to customise section header with static tableView
ios uitableview header
ios uitableview header
asked Nov 22 '18 at 9:05


Vineesh TPVineesh TP
4,77754272
4,77754272
Could you please clarify what do you mean by static tableView?
– Gkolunia
Nov 22 '18 at 9:10
@Gkolunia: There are to type of tableview static cells and dynamic cells. I am using Static cell for UITableView
– Vineesh TP
Nov 22 '18 at 9:27
add a comment |
Could you please clarify what do you mean by static tableView?
– Gkolunia
Nov 22 '18 at 9:10
@Gkolunia: There are to type of tableview static cells and dynamic cells. I am using Static cell for UITableView
– Vineesh TP
Nov 22 '18 at 9:27
Could you please clarify what do you mean by static tableView?
– Gkolunia
Nov 22 '18 at 9:10
Could you please clarify what do you mean by static tableView?
– Gkolunia
Nov 22 '18 at 9:10
@Gkolunia: There are to type of tableview static cells and dynamic cells. I am using Static cell for UITableView
– Vineesh TP
Nov 22 '18 at 9:27
@Gkolunia: There are to type of tableview static cells and dynamic cells. I am using Static cell for UITableView
– Vineesh TP
Nov 22 '18 at 9:27
add a comment |
2 Answers
2
active
oldest
votes
The problem is you should return a UIView
, not a UITableViewCell
in viewForHeaderInSection
method.
Also you should keep an instance of header cell for future use (like modifying its view)
Create a field in your TableViewController
like this:
private var headerViewCell: UITableViewCell?
and then in the viewForHeaderInSection
do this:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
headerCell?.textLabel?.text = "Section (section + 1)"
headerCell?.textLabel?.textColor = UIColor.blue
self.headerViewCell = headerCell
return headerCell?.contentView
}
I could you 'UITableViewCell' for Customise section header. but it is not working in static tableview
– Vineesh TP
Nov 22 '18 at 9:24
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here
– Devil Decoder
Nov 22 '18 at 10:11
I assume you will edit the header cell view such as text color, text label or whatever. in this case wired things will happen! such as disappearing header etc. I experienced this type of issues and all of them was because I returned theUITableViewCell
itself! So instead of down vote, please do some research. beside, it's nice to use Google Translate
– Sepehr Behroozi
Nov 22 '18 at 12:18
add a comment |
I think you should unwrap cell when using dequeueReusableCell
.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! HeaderCell // here header cell is your cell's custom class
headerCell.textLabel.text = "Section (section + 1)"
headerCell.textLabel.textColor = UIColor.blue
return headerCell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here there no need for unwraping
– Devil Decoder
Nov 22 '18 at 10:11
well i for custom headerview you need to return a custom cell or a custom view
– Abu Ul Hassan
Nov 22 '18 at 10:22
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%2f53427244%2fcustomise-static-tableview-header-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is you should return a UIView
, not a UITableViewCell
in viewForHeaderInSection
method.
Also you should keep an instance of header cell for future use (like modifying its view)
Create a field in your TableViewController
like this:
private var headerViewCell: UITableViewCell?
and then in the viewForHeaderInSection
do this:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
headerCell?.textLabel?.text = "Section (section + 1)"
headerCell?.textLabel?.textColor = UIColor.blue
self.headerViewCell = headerCell
return headerCell?.contentView
}
I could you 'UITableViewCell' for Customise section header. but it is not working in static tableview
– Vineesh TP
Nov 22 '18 at 9:24
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here
– Devil Decoder
Nov 22 '18 at 10:11
I assume you will edit the header cell view such as text color, text label or whatever. in this case wired things will happen! such as disappearing header etc. I experienced this type of issues and all of them was because I returned theUITableViewCell
itself! So instead of down vote, please do some research. beside, it's nice to use Google Translate
– Sepehr Behroozi
Nov 22 '18 at 12:18
add a comment |
The problem is you should return a UIView
, not a UITableViewCell
in viewForHeaderInSection
method.
Also you should keep an instance of header cell for future use (like modifying its view)
Create a field in your TableViewController
like this:
private var headerViewCell: UITableViewCell?
and then in the viewForHeaderInSection
do this:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
headerCell?.textLabel?.text = "Section (section + 1)"
headerCell?.textLabel?.textColor = UIColor.blue
self.headerViewCell = headerCell
return headerCell?.contentView
}
I could you 'UITableViewCell' for Customise section header. but it is not working in static tableview
– Vineesh TP
Nov 22 '18 at 9:24
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here
– Devil Decoder
Nov 22 '18 at 10:11
I assume you will edit the header cell view such as text color, text label or whatever. in this case wired things will happen! such as disappearing header etc. I experienced this type of issues and all of them was because I returned theUITableViewCell
itself! So instead of down vote, please do some research. beside, it's nice to use Google Translate
– Sepehr Behroozi
Nov 22 '18 at 12:18
add a comment |
The problem is you should return a UIView
, not a UITableViewCell
in viewForHeaderInSection
method.
Also you should keep an instance of header cell for future use (like modifying its view)
Create a field in your TableViewController
like this:
private var headerViewCell: UITableViewCell?
and then in the viewForHeaderInSection
do this:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
headerCell?.textLabel?.text = "Section (section + 1)"
headerCell?.textLabel?.textColor = UIColor.blue
self.headerViewCell = headerCell
return headerCell?.contentView
}
The problem is you should return a UIView
, not a UITableViewCell
in viewForHeaderInSection
method.
Also you should keep an instance of header cell for future use (like modifying its view)
Create a field in your TableViewController
like this:
private var headerViewCell: UITableViewCell?
and then in the viewForHeaderInSection
do this:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell")
headerCell?.textLabel?.text = "Section (section + 1)"
headerCell?.textLabel?.textColor = UIColor.blue
self.headerViewCell = headerCell
return headerCell?.contentView
}
answered Nov 22 '18 at 9:11


Sepehr BehrooziSepehr Behroozi
1,2661127
1,2661127
I could you 'UITableViewCell' for Customise section header. but it is not working in static tableview
– Vineesh TP
Nov 22 '18 at 9:24
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here
– Devil Decoder
Nov 22 '18 at 10:11
I assume you will edit the header cell view such as text color, text label or whatever. in this case wired things will happen! such as disappearing header etc. I experienced this type of issues and all of them was because I returned theUITableViewCell
itself! So instead of down vote, please do some research. beside, it's nice to use Google Translate
– Sepehr Behroozi
Nov 22 '18 at 12:18
add a comment |
I could you 'UITableViewCell' for Customise section header. but it is not working in static tableview
– Vineesh TP
Nov 22 '18 at 9:24
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here
– Devil Decoder
Nov 22 '18 at 10:11
I assume you will edit the header cell view such as text color, text label or whatever. in this case wired things will happen! such as disappearing header etc. I experienced this type of issues and all of them was because I returned theUITableViewCell
itself! So instead of down vote, please do some research. beside, it's nice to use Google Translate
– Sepehr Behroozi
Nov 22 '18 at 12:18
I could you 'UITableViewCell' for Customise section header. but it is not working in static tableview
– Vineesh TP
Nov 22 '18 at 9:24
I could you 'UITableViewCell' for Customise section header. but it is not working in static tableview
– Vineesh TP
Nov 22 '18 at 9:24
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here
– Devil Decoder
Nov 22 '18 at 10:11
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here
– Devil Decoder
Nov 22 '18 at 10:11
I assume you will edit the header cell view such as text color, text label or whatever. in this case wired things will happen! such as disappearing header etc. I experienced this type of issues and all of them was because I returned the
UITableViewCell
itself! So instead of down vote, please do some research. beside, it's nice to use Google Translate– Sepehr Behroozi
Nov 22 '18 at 12:18
I assume you will edit the header cell view such as text color, text label or whatever. in this case wired things will happen! such as disappearing header etc. I experienced this type of issues and all of them was because I returned the
UITableViewCell
itself! So instead of down vote, please do some research. beside, it's nice to use Google Translate– Sepehr Behroozi
Nov 22 '18 at 12:18
add a comment |
I think you should unwrap cell when using dequeueReusableCell
.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! HeaderCell // here header cell is your cell's custom class
headerCell.textLabel.text = "Section (section + 1)"
headerCell.textLabel.textColor = UIColor.blue
return headerCell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here there no need for unwraping
– Devil Decoder
Nov 22 '18 at 10:11
well i for custom headerview you need to return a custom cell or a custom view
– Abu Ul Hassan
Nov 22 '18 at 10:22
add a comment |
I think you should unwrap cell when using dequeueReusableCell
.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! HeaderCell // here header cell is your cell's custom class
headerCell.textLabel.text = "Section (section + 1)"
headerCell.textLabel.textColor = UIColor.blue
return headerCell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here there no need for unwraping
– Devil Decoder
Nov 22 '18 at 10:11
well i for custom headerview you need to return a custom cell or a custom view
– Abu Ul Hassan
Nov 22 '18 at 10:22
add a comment |
I think you should unwrap cell when using dequeueReusableCell
.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! HeaderCell // here header cell is your cell's custom class
headerCell.textLabel.text = "Section (section + 1)"
headerCell.textLabel.textColor = UIColor.blue
return headerCell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
I think you should unwrap cell when using dequeueReusableCell
.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! HeaderCell // here header cell is your cell's custom class
headerCell.textLabel.text = "Section (section + 1)"
headerCell.textLabel.textColor = UIColor.blue
return headerCell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
answered Nov 22 '18 at 9:49
Abu Ul HassanAbu Ul Hassan
285218
285218
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here there no need for unwraping
– Devil Decoder
Nov 22 '18 at 10:11
well i for custom headerview you need to return a custom cell or a custom view
– Abu Ul Hassan
Nov 22 '18 at 10:22
add a comment |
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here there no need for unwraping
– Devil Decoder
Nov 22 '18 at 10:11
well i for custom headerview you need to return a custom cell or a custom view
– Abu Ul Hassan
Nov 22 '18 at 10:22
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here there no need for unwraping
– Devil Decoder
Nov 22 '18 at 10:11
it return an optional view and tablview cell is also a subclass of uiview and upcasting in implicit so you can return uitableview cell here there no need for unwraping
– Devil Decoder
Nov 22 '18 at 10:11
well i for custom headerview you need to return a custom cell or a custom view
– Abu Ul Hassan
Nov 22 '18 at 10:22
well i for custom headerview you need to return a custom cell or a custom view
– Abu Ul Hassan
Nov 22 '18 at 10:22
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%2f53427244%2fcustomise-static-tableview-header-swift%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
Could you please clarify what do you mean by static tableView?
– Gkolunia
Nov 22 '18 at 9:10
@Gkolunia: There are to type of tableview static cells and dynamic cells. I am using Static cell for UITableView
– Vineesh TP
Nov 22 '18 at 9:27