#define User_key @"users"
#define Groupname_key @"GROUPNAME"
@interface Viewcontroller () < Uitableviewdatasource , uitableviewdelegate >
{
UITableView *_tableview;
Nsmutablearray *_datasource;
BOOL expanded;
}
-(void) Initializedatasource;
-(void) Initializeuserinterface;
-(void) buttonpressed: (UIButton *) sender;
-(BOOL) isexpanded: (int) Section;
-(void) Cellapseorexpand: (int) Section;
@end
@implementation Viewcontroller
-(void) Dealloc
{
[_tableviewrelease];
[_datasourcerelease];
[Super dealloc];
}
-(void) viewdidload
{
[Superviewdidload];
Do any additional setup after loading the view.
[self initializedatasource];
[self initializeuserinterface];
}
-(void) Initializedatasource
{
_datasource = [[nsmutablearrayarray] retain];
nsmutabledictionary *dict = [[nsmutabledictionaryalloc] init];
[Dict setobject:@ "Friend" forkey:groupname_key];
nsmutablearray *arr = [[nsmutablearrayalloc] init];
[Arr addobject: @ "Amanda"];
[Arr addobject: @ "Cecil"];
[Arr addobject: @ "Martin"];
[Dict setobject: arr forkey:user_key];
[Arr release];
[_datasource addobject:d ICT];
[dict release];
nsmutabledictionary *diction = [[nsmutabledictionaryalloc]init];
[Diction setobject:@ "workmmate" forkey:groupname_key];
nsmutablearray *array = [[nsmutablearrayalloc] init];
[Array addobject: @ "Eartha"];
[Array addobject: @ "Deirdre"];
[Array addobject: @ "Montague"];
[Array addobject:@ "Ken"];
[Diction setobject: Array forkey:user_key];
[Array release];
[_datasource addobject:d iction];
[Diction release];
}
-(void) Initializeuserinterface
{
self. View. BackgroundColor = [uicolorwhitecolor];
_tableview = [[uitableviewalloc]initwithframe: Self . View. Boundsstyle:uitableviewstyleplain];
_tableview. Delegate = Self ;
_tableview. DataSource = Self ;
[self. Viewaddsubview:_tableview];
[_tableviewrelease];
[_tableviewreloaddata];
}
-(void) Cellapseorexpand: (int) Section
{
expanded = NO;
nsmutabledictionary *data = [_datasourceobjectatindex: section];
if ([Data objectforkey:@ "expanded"]! = Nil)
{
expanded = [[Data Objectforkey:@ "expanded" ] boolvalue];
}
[Data setobject: [nsnumbernumberwithbool:! expanded ] forkey:@ "expanded"];
}
-(BOOL) isexpanded: (int) Section
{
expanded = NO;
nsmutabledictionary *data = [_datasourceobjectatindex: section];
if ([Data objectforkey:@ "expanded"]! = Nil)
{
expanded = [[Data Objectforkey:@ "expanded" ] intvalue];
}
returnexpanded;
}
#pragma make-<UITableViewDelegate>
-(cgfloat) TableView: (uitableview *) TableView heightforheaderinsection: (nsinteger) Section
{
return ;
}
-(Nsinteger) Numberofsectionsintableview: (uitableview *) TableView
{
return [_datasourceCount];
}
-(Nsinteger) TableView: (uitableview *) TableView numberofrowsinsection: (nsinteger) Section
{
// current is collapsed
if (! [ self isexpanded: section])
{
return 0;
}
// returns data for the current node
nsdictionary *data = [_datasource objectatindex: section];
return [[Data objectforkey:user_key]count];
}
-(UIView *) TableView: (uitableview *) TableView viewforheaderinsection: (nsinteger) Section
{
UIView *heighview = [[uiviewalloc]initwithframe:cgrectmake(0 , 0, +)];
Heighview. BackgroundColor = [uicolorcolorwithpatternimage: [uiimageimagenamed:@] Line.png " ];
UILabel *label = [[uilabelalloc]init];
Label. bounds = cgrectmake(0, 0, -- );
Label. Center = cgpointmake(+) ;
nsdictionary *data = [_datasource objectatindex: section];
Label. text = [Data objectforkey:groupname_key];
label. TextAlignment = nstextalignmentleft;
label. BackgroundColor = [uicolorclearcolor];
[Heighview addsubview: label];
UIButton *button = [[uibuttonalloc]initwithframe:cgrectmake(0 , 0, (+))];
[Button addTarget:self action:@selector(buttonpressed:) forControlEvents:uicontroleventtouchupinside];
// Save knot Number, delivery method
button. tag = section;
// whether to expand, toggle button to display picture
if ([ self isexpanded: section])
{
[Button setimage: [uiimageimagenamed:@ "Btn_down"] forstate: UIControlStateNormal];
}
Else
{
[Button setimage: [uiimageimagenamed:@ "Btn_right"] forstate< c13>:uicontrolstatenormal];
}
[Heighview addsubview: button];
[Button release];
return Heighview;
}
-(uitableviewcell *) TableView: (uitableview *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
{
Static nsstring *cellindentifer = @ "cell";
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier: Cellindentifer];
if (!cell)
{
cell = [[[Uitableviewcellalloc]initwithstyle: Uitableviewcellstyledefaultreuseidentifier: cellindentifer]autorelease];
}
nsdictionary *data = (nsdictionary *) [_datasourceobjectatindex: indexpath.< /c9>section];
nsarray *personname = (nsarray *) [Data objectforkey:user_key];
if (PersonName = = Nil)
{
return cell;
}
cell. Selectionstyle = Uitableviewcellselectionstylegray;
Cell. Textlabel. text = [PersonName objectatindex: Indexpath. Row];
return cell;
}
-(void) buttonpressed: (UIButton *) sender
{
[ self cellapseorexpand: Sender. tag];
[_tableviewreloaddata];
}
@end