Bhavuk Jain
Bhavuk Jain

Reputation: 2187

Get indexPath from custom Tableview section header

I have created a custom View and added it to the tableView section header.

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

        if section == 2 {

            if (headerView == nil) {
                headerView = CustomHeaderSection(frame: CGRectMake(0,0,self.view.bounds.width,38))
            }
            headerView!.delegate = self

            if isExpandableCell {

                headerView!.arrowImage.transform = CGAffineTransformIdentity

            }else {

                headerView!.arrowImage.transform = CGAffineTransformMakeRotation(CGFloat(M_PI))

            }
            return headerView

        }

        return nil
    }

I have a button on the custom view that I have added on the tableview section. Now how will I get the indexPath when I click on it?

Upvotes: 5

Views: 3725

Answers (3)

Saurav
Saurav

Reputation: 538

Declare a section number variable in CustomHeader class and assign its value while instantiating inside viewForHeaderInSection().

Implement the target button action inside the same class. So, on tap get self.section and pass it to ViewController.

void onButtonTap (id sender)
        {
            UIButton button = (UIButton)sender;

        this.sectionHeaderViewDelegate.buttonTapped (this.section);

        }

Upvotes: 1

NLCJohn
NLCJohn

Reputation: 1

On your function you can add a tag id to the button to be the same as the indexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FoldingCell", forIndexPath: indexPath) as! myTableViewCell
...
cell.myButton.tag = indexPath.row
...
}

Upvotes: 0

Alexander Doloz
Alexander Doloz

Reputation: 4198

You can assign the tag property of your section header view to number of section inside tableView(tableView: UITableView, viewForHeaderInSection section: Int) method.

Upvotes: -1

Related Questions