Access Each Header and Controls in The Tableview in Swift

Access each header and controls in the tableview in swift

In order to get button and image from header, you can implement a customized UIView for header, like

class HeaderView: UIView {
var button: UIButton
var image: UIImage

func init() {
...
}
}

And return this HeaderView in your tableView(tableView: UITableView, viewForHeaderInSection section: Int) callback.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return HeaderView()
}

Then use tableView.headerViewForSection(section:) to get 5 customized headers view.

func getHeaders() {

for index in 1...5 {
let header = tableView.headerViewForSection(section: index) as! HeaderView
let button = header.button
let image = header.image
}
}

How to access footer or header view of specific section in UITableView?

You could tag your custom section footer view when you create it, e.g., in tableView:viewForFooterInSection:, like this:

mySectionFooterView.tag = kMySectionFooterViewTag;

kMySectionFooterViewTag can be any NSInteger you like.

To access mySectionFooterView from some other part of your program you can then use:

mySectionFooterView = [myTableView viewWithTag:kMySectionFooterViewTag];

myTableView is the UITableView that includes your custom section footer view. You can typically access myTableView as self.tableView, when using a UITableViewController.

Note that there can be performance considerations with this approach, because viewWithTag: will search the entire view hierarchy of myTableView.

Accessing UITableView Header View

After reading the post in this question, I got a hint and found out that, in func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? method, I should return a UITableViewHeaderFooterView instead of the previously UIView. As I changed the returned object's type to UITableViewHeaderFooterView, with everything else remaining the same, (i.e. add a UITextField to the UITableViewHeaderFooterView) I managed to access the UITextField in the UITableViewHeaderFooterView via headerView(forSection:) method.

To Be even clearer, given in the apple's documentation that the function declaration is func headerView(forSection section: Int) -> UITableViewHeaderFooterView?, showing that the returned value is UITableViewHeaderFooterView, in order to access the header view using the method, the returned value from func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? should be UITableViewHeaderFooterView instead of any other generic UIView or UIView subclasses.

Hope this helps.

how to get tableView section header

If the headerview is not on the screen (not visible), you cannot reach to it.

Maybe storyboard can help you. Drag an UIView into the tableview. This will be your headerview and you can connect controls directly to the UIViewController and access them.

Also you don't need to use viewForHeaderInSection for this example.

headerview in storyboard

How can I access the rows in sections in my container?

It looks like you have not connected your tableView delegate to your viewController either in Interface Builder, or in code, in you viewDidLoad for instance:

myTableView.delegate = self

EDIT: Don't forget to make your view controller to conform to UITableViewDelegate

class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { ... } 

UITableview get index of each section

An easy way is to use the section number as tag of the button

deleteButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
deleteButton.tag = section

In the action get the section index and delete the section

@objc func buttonTapped(_ sender : UIButton) {
let section = sender.tag
bookings.remove(at: section)
tableView.deleteSections([section], with: .fade)
}

Customizing Header and Footer of Table View with Swift

There are several ways to do that. Most of do that by programatically and some achieve it via storyboard and program.

so its totally depends on you how you want to achieve it.

I can share a easiest way with you to customise your header and footer section.

if you have good control on storyboard try to create one

UITableViewCell 

Now decorate it as you want and in identifier Name it SectionHeader now use it as reuse cell

After that use this Delegate Method i am sharing an objective C delegate,

        override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

var headerView: SectionHeaderTableViewCell? = tableView.dequeueReusableCellWithIdentifier("SectionHeader")
if (headerView == nil) {
headerView = SectionHeaderTableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier:"SectionHeader")
}
headerView!.textLabel!.text = "Hello World"

return headerView;

}

now do same thing for footer.

access tableView header added using storyboard

Even though your View object is acting like TableView header but still you can control it with IBOutlet connection. Add an IBOutlet in your UIViewController like following

@IBOutlet weak var tableHeader: UIView!

and add following orientation method to hide/unhide the view with frame change.

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape {
self.tableHeader.frame = CGRect(x: 0, y: 0, width: self.tableHeader.frame.width, height: 0)
} else {
self.tableHeader.frame = CGRect(x: 0, y: 0, width: self.tableHeader.frame.width, height: 40)
}
}

Can't Interact With Controls In Table View Header

The height of your header view is 65 points. You're inserting your searchbar at Y=65 so it's beyond the bounds of the header rect. Move your searchbar to Y=0 and your segmented control below it, and it's going to work just fine.

Have a nice day :)



Related Topics



Leave a reply



Submit