Reputation: 501
I have UITableView
in which I have added a UIButton
. The problem is it is only showing in on the top of the header. How can I add the button in all of the section headers so that I can make the section interactive?
Here is the code for adding a button on Section Header of UITableView
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor(red: 255/255, green: 200/255, blue: 20/255, alpha: 1.0) // Just some random color
header.textLabel!.textColor = UIColor.whiteColor()
let btn = UIButton(type: UIButtonType.Custom) as UIButton
btn.frame = CGRectMake(header.frame.width - 62, 0, 50, 50)
btn.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
btn.setTitle("Direction", forState: .Normal)
btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
header.contentView.addSubview(btn)
}
Can anyone tell me how to add interactive button on every section?
Upvotes: 2
Views: 1990
Reputation: 699
You Can use Simply this delegate Method Of Tableview :
optional public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
Upvotes: 0
Reputation: 16543
You have to use this delegate
optional public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
to customize the headerview section of tableview
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let header: = UIView()
let btn = UIButton(type: UIButtonType.Custom) as UIButton
btn.frame = CGRectMake(header.frame.width - 62, 0, 50, 50)
btn.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
btn.setTitle("Direction", forState: .Normal)
btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
header.addSubview(btn)
return header
}
Upvotes: 2