How can we add image in UITableView section header using swift?
In my willDisplayHeaderView I have changed color of section header But I want to add an image before section title. Any help?
My code for willDisplayHeaderView is
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor(red: 238/255, green: 168/255, blue: 15/255, alpha: 0.8)
header.textLabel.textColor = UIColor.whiteColor()
header.alpha = 1
}
ios uitableview swift
add a comment |
In my willDisplayHeaderView I have changed color of section header But I want to add an image before section title. Any help?
My code for willDisplayHeaderView is
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor(red: 238/255, green: 168/255, blue: 15/255, alpha: 0.8)
header.textLabel.textColor = UIColor.whiteColor()
header.alpha = 1
}
ios uitableview swift
add a comment |
In my willDisplayHeaderView I have changed color of section header But I want to add an image before section title. Any help?
My code for willDisplayHeaderView is
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor(red: 238/255, green: 168/255, blue: 15/255, alpha: 0.8)
header.textLabel.textColor = UIColor.whiteColor()
header.alpha = 1
}
ios uitableview swift
In my willDisplayHeaderView I have changed color of section header But I want to add an image before section title. Any help?
My code for willDisplayHeaderView is
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor(red: 238/255, green: 168/255, blue: 15/255, alpha: 0.8)
header.textLabel.textColor = UIColor.whiteColor()
header.alpha = 1
}
ios uitableview swift
ios uitableview swift
asked Feb 19 '15 at 7:00
Saqib Omer
3,40443956
3,40443956
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
You could design your own header, like you would design a custom cell in a table view.
Or you could just add an image like so:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var myCustomView: UIImageView
var myImage: UIImage = UIImage(named: "myImageResource")
myCustomView.image = myImage
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.addSubview(myCustomView)
return header
}
Then you can simply add your section title UILabel as another subview.
5
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
– Mikael
Feb 23 '16 at 2:11
I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
– Parth Barot
May 23 at 6:42
Just initialize myCustomView asvar myCustomView: UIImageView = UIimageView(). An error will disappear
– shripad20
Aug 1 at 14:00
add a comment |
Simply Add the following code in your viewDidLoad() method.
var frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
var headerImageView = UIImageView(frame: frame)
var image: UIImage = UIImage(named: "yourImage")!
headerImageView.image = image
youTableView.tableHeaderView = headerImageView
add a comment |
Here it is how we can add custom header in UITableView.
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
let imageViewGame = UIImageView(frame: CGRectMake(5, 8, 40, 40));
let image = UIImage(named: "Games.png");
imageViewGame.image = image;
header.contentView.addSubview(imageViewGame)
}
add a comment |
For Swift 3
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
let headerImage = UIImage(named: "headerBanner.jpg")
let headerImageView = UIImageView(image: headerImage)
header.backgroundView = headerImageView
}
add a comment |
I use this method below in my proj.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let uilbl = UILabel()
//uilbl.numberOfLines = 0
//uilbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
//uilbl.text = "blablabla"
uilbl.sizeToFit()
uilbl.backgroundColor = UIColor(patternImage: UIImage(named: "yr-img-name")!)
return uilbl
}
add a comment |
This will work with swift 4
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerImage: UIImage = UIImage(named: "image")!
let headerView = UIImageView(image: headerImage)
return headerView
}
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%2f28600375%2fhow-can-we-add-image-in-uitableview-section-header-using-swift%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could design your own header, like you would design a custom cell in a table view.
Or you could just add an image like so:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var myCustomView: UIImageView
var myImage: UIImage = UIImage(named: "myImageResource")
myCustomView.image = myImage
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.addSubview(myCustomView)
return header
}
Then you can simply add your section title UILabel as another subview.
5
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
– Mikael
Feb 23 '16 at 2:11
I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
– Parth Barot
May 23 at 6:42
Just initialize myCustomView asvar myCustomView: UIImageView = UIimageView(). An error will disappear
– shripad20
Aug 1 at 14:00
add a comment |
You could design your own header, like you would design a custom cell in a table view.
Or you could just add an image like so:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var myCustomView: UIImageView
var myImage: UIImage = UIImage(named: "myImageResource")
myCustomView.image = myImage
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.addSubview(myCustomView)
return header
}
Then you can simply add your section title UILabel as another subview.
5
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
– Mikael
Feb 23 '16 at 2:11
I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
– Parth Barot
May 23 at 6:42
Just initialize myCustomView asvar myCustomView: UIImageView = UIimageView(). An error will disappear
– shripad20
Aug 1 at 14:00
add a comment |
You could design your own header, like you would design a custom cell in a table view.
Or you could just add an image like so:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var myCustomView: UIImageView
var myImage: UIImage = UIImage(named: "myImageResource")
myCustomView.image = myImage
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.addSubview(myCustomView)
return header
}
Then you can simply add your section title UILabel as another subview.
You could design your own header, like you would design a custom cell in a table view.
Or you could just add an image like so:
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var myCustomView: UIImageView
var myImage: UIImage = UIImage(named: "myImageResource")
myCustomView.image = myImage
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.addSubview(myCustomView)
return header
}
Then you can simply add your section title UILabel as another subview.
answered Feb 19 '15 at 7:47
Anfaje
215212
215212
5
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
– Mikael
Feb 23 '16 at 2:11
I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
– Parth Barot
May 23 at 6:42
Just initialize myCustomView asvar myCustomView: UIImageView = UIimageView(). An error will disappear
– shripad20
Aug 1 at 14:00
add a comment |
5
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
– Mikael
Feb 23 '16 at 2:11
I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
– Parth Barot
May 23 at 6:42
Just initialize myCustomView asvar myCustomView: UIImageView = UIimageView(). An error will disappear
– shripad20
Aug 1 at 14:00
5
5
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
– Mikael
Feb 23 '16 at 2:11
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView This simply doesn't work here...
– Mikael
Feb 23 '16 at 2:11
I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
– Parth Barot
May 23 at 6:42
I have added this myCustomView programmatically but it's not showing any image while running?? @Anfaje
– Parth Barot
May 23 at 6:42
Just initialize myCustomView as
var myCustomView: UIImageView = UIimageView(). An error will disappear– shripad20
Aug 1 at 14:00
Just initialize myCustomView as
var myCustomView: UIImageView = UIimageView(). An error will disappear– shripad20
Aug 1 at 14:00
add a comment |
Simply Add the following code in your viewDidLoad() method.
var frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
var headerImageView = UIImageView(frame: frame)
var image: UIImage = UIImage(named: "yourImage")!
headerImageView.image = image
youTableView.tableHeaderView = headerImageView
add a comment |
Simply Add the following code in your viewDidLoad() method.
var frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
var headerImageView = UIImageView(frame: frame)
var image: UIImage = UIImage(named: "yourImage")!
headerImageView.image = image
youTableView.tableHeaderView = headerImageView
add a comment |
Simply Add the following code in your viewDidLoad() method.
var frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
var headerImageView = UIImageView(frame: frame)
var image: UIImage = UIImage(named: "yourImage")!
headerImageView.image = image
youTableView.tableHeaderView = headerImageView
Simply Add the following code in your viewDidLoad() method.
var frame = CGRectMake(0, 0, self.view.frame.size.width, 200)
var headerImageView = UIImageView(frame: frame)
var image: UIImage = UIImage(named: "yourImage")!
headerImageView.image = image
youTableView.tableHeaderView = headerImageView
edited Jun 18 '15 at 3:15
Ram
2,57993151
2,57993151
answered Jun 11 '15 at 9:48
Muhammad Jabbar
393515
393515
add a comment |
add a comment |
Here it is how we can add custom header in UITableView.
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
let imageViewGame = UIImageView(frame: CGRectMake(5, 8, 40, 40));
let image = UIImage(named: "Games.png");
imageViewGame.image = image;
header.contentView.addSubview(imageViewGame)
}
add a comment |
Here it is how we can add custom header in UITableView.
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
let imageViewGame = UIImageView(frame: CGRectMake(5, 8, 40, 40));
let image = UIImage(named: "Games.png");
imageViewGame.image = image;
header.contentView.addSubview(imageViewGame)
}
add a comment |
Here it is how we can add custom header in UITableView.
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
let imageViewGame = UIImageView(frame: CGRectMake(5, 8, 40, 40));
let image = UIImage(named: "Games.png");
imageViewGame.image = image;
header.contentView.addSubview(imageViewGame)
}
Here it is how we can add custom header in UITableView.
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
let imageViewGame = UIImageView(frame: CGRectMake(5, 8, 40, 40));
let image = UIImage(named: "Games.png");
imageViewGame.image = image;
header.contentView.addSubview(imageViewGame)
}
edited Apr 22 '17 at 14:22
answered Feb 19 '15 at 8:01
Saqib Omer
3,40443956
3,40443956
add a comment |
add a comment |
For Swift 3
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
let headerImage = UIImage(named: "headerBanner.jpg")
let headerImageView = UIImageView(image: headerImage)
header.backgroundView = headerImageView
}
add a comment |
For Swift 3
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
let headerImage = UIImage(named: "headerBanner.jpg")
let headerImageView = UIImageView(image: headerImage)
header.backgroundView = headerImageView
}
add a comment |
For Swift 3
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
let headerImage = UIImage(named: "headerBanner.jpg")
let headerImageView = UIImageView(image: headerImage)
header.backgroundView = headerImageView
}
For Swift 3
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
let headerImage = UIImage(named: "headerBanner.jpg")
let headerImageView = UIImageView(image: headerImage)
header.backgroundView = headerImageView
}
answered Mar 16 '17 at 9:38
Arvin Sanmuga Rajah
363310
363310
add a comment |
add a comment |
I use this method below in my proj.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let uilbl = UILabel()
//uilbl.numberOfLines = 0
//uilbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
//uilbl.text = "blablabla"
uilbl.sizeToFit()
uilbl.backgroundColor = UIColor(patternImage: UIImage(named: "yr-img-name")!)
return uilbl
}
add a comment |
I use this method below in my proj.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let uilbl = UILabel()
//uilbl.numberOfLines = 0
//uilbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
//uilbl.text = "blablabla"
uilbl.sizeToFit()
uilbl.backgroundColor = UIColor(patternImage: UIImage(named: "yr-img-name")!)
return uilbl
}
add a comment |
I use this method below in my proj.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let uilbl = UILabel()
//uilbl.numberOfLines = 0
//uilbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
//uilbl.text = "blablabla"
uilbl.sizeToFit()
uilbl.backgroundColor = UIColor(patternImage: UIImage(named: "yr-img-name")!)
return uilbl
}
I use this method below in my proj.
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let uilbl = UILabel()
//uilbl.numberOfLines = 0
//uilbl.lineBreakMode = NSLineBreakMode.ByWordWrapping
//uilbl.text = "blablabla"
uilbl.sizeToFit()
uilbl.backgroundColor = UIColor(patternImage: UIImage(named: "yr-img-name")!)
return uilbl
}
edited Oct 31 '15 at 9:44
Hitesh
2,11262541
2,11262541
answered Oct 31 '15 at 8:51
Nik
1017
1017
add a comment |
add a comment |
This will work with swift 4
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerImage: UIImage = UIImage(named: "image")!
let headerView = UIImageView(image: headerImage)
return headerView
}
add a comment |
This will work with swift 4
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerImage: UIImage = UIImage(named: "image")!
let headerView = UIImageView(image: headerImage)
return headerView
}
add a comment |
This will work with swift 4
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerImage: UIImage = UIImage(named: "image")!
let headerView = UIImageView(image: headerImage)
return headerView
}
This will work with swift 4
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerImage: UIImage = UIImage(named: "image")!
let headerView = UIImageView(image: headerImage)
return headerView
}
answered Mar 9 at 15:13
kroky
429311
429311
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f28600375%2fhow-can-we-add-image-in-uitableview-section-header-using-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