Customise static tableview Header - Swift












0















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










share|improve this question























  • 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
















0















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










share|improve this question























  • 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














0












0








0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












2 Answers
2






active

oldest

votes


















-1














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
}





share|improve this answer
























  • 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 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



















-1














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
}





share|improve this answer
























  • 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













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
});


}
});














draft saved

draft discarded


















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









-1














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
}





share|improve this answer
























  • 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 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
















-1














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
}





share|improve this answer
























  • 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 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














-1












-1








-1







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
}





share|improve this answer













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
}






share|improve this answer












share|improve this answer



share|improve this answer










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 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 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 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 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













-1














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
}





share|improve this answer
























  • 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


















-1














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
}





share|improve this answer
























  • 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
















-1












-1








-1







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
}





share|improve this answer













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
}






share|improve this answer












share|improve this answer



share|improve this answer










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





















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith