views:

38

answers:

1

Hello,

I am currently creating a UITableView with expandable/collapsable sections.

I get data from the Internet in JSON format, storing it in arrays just like this :

{Section 1 {s1 data 1, s1 data 2, ... }, Section 2 {s2 data 1, s2 data 2, and so on}}

Sections can be alphabetical letters, year number or whatever.

I create my table view headers like this :

GTHeaderView *header = [GTHeaderView headerViewWithTitle:[NSString stringWithFormat:@"%@", myArray.SectionValue]];
[header.button addTarget:self action:@selector(toggleSection) forControlEvents:UIControlEventTouchUpInside];

This is fine, but in my "toggleSection" method, I can't find a way to know witch section have been touched to be collapsed/expanded.

I know I can't send parameters in selector... so what solution do you think would fit my needs ?

Thanks in advance !

+1  A: 

You can write your toggleSection method as:

- (void)toggleSection:(id)sender
{
   // send the sender object a message to find out the section number here...
}

and of course change your addTarget line to specify that toggleSection method now has one parameter:

[header.button addTarget:self action:@selector(toggleSection:) forControlEvents:UIControlEventTouchUpInside];
petert
thank you for your answer, the sender object returns me a UIButton instead of a GTHeaderView. So I can't get information like the section label. Any idea ?
Dough
Now I see what this `GTHeaderView` class is for, I found this link: http://cocoaminded.com/2010/02/06/expandingcollapsing-uitableview-sections/ - it shows the simple case where you have separate methods for each section; this would be okay if you have a fixed or small number of sections I guess.
petert
I managed to know witch section is touched by setting the tag in GTHeaderView's UIButton like this : header.button.tag = i; working fine, many thanks !!
Dough