Function: Click cell to display the details of the cell, and click Close cell details again.
# Import <uikit/uikit. h>
@ Interface mycell: uitableviewcell
{
Uilabel * lab_info;
Uilabel * lab_detailinfo;
Cgfloat normalheight;
}
@ Property (retain, nonatomic) uilabel * lab_info, * lab_detailinfo;
@ Property cgfloat normalheight;
-(Cgfloat) getmycellheight;
-(Void) resetcellheight;
@ End
# Import "mycell. H"
@ Interface mycell ()
{
}
@ End
@ Implementation mycell
@ Synthesize lab_info, lab_detailinfo;
@ Synthesize normalheight;
-(ID) initwithstyle :( uitableviewcellstyle) style reuseidentifier :( nsstring *) reuseidentifier
{
Self = [Super initwithstyle: style reuseidentifier: reuseidentifier];
If (Self ){
Lab_info = [[uilabel alloc] init];
[Self. contentview addsubview: lab_info];
//
Self. normalheight = self. Frame. Size. height;
//
Lab_detailinfo = [[uilabel alloc] init];
Lab_detailinfo.linebreakmode = nslinebreakbycharwrapping;
Lab_detailinfo.numberoflines = 5;
[Self. contentview addsubview: lab_detailinfo];
//
Self. layer. shadowcolor = [uicolor blackcolor]. cgcolor;
Self. layer. shadowoffset = cgsizemake (0, 2 );
Self. layer. shadowopacity = 0.3;
Self. layer. shadowradius = 2;
//
}
Return self;
}
-(Void) setselected :( bool) selected animated :( bool) animated
{
[Super setselected: Selected animated: animated];
}
-(Cgfloat) getdetaillabviewheight
{
[Self. lab_detailinfo sizetofit];
Return self. lab_detailinfo.frame.size.height ;;
}
-(Cgfloat) getmycellheight
{
Return normalheight + [self getdetaillabviewheight];
}
-(Void) resetcellheight
{
[Self layoutifneeded];
Cgrect cellframe = self. frame;
Cellframe. Size. Height = normalheight + [self getdetaillabviewheight];
[Self setframe: cellframe];
}
-(Void) layoutsubviews
{
[Super layoutsubviews];
// Set the lab height of the title
Cgrect lab_info_frame = cgrectinset (self. Frame, 0, 0 );
Lab_info_frame.origin.x = lab_info_frame.origin.y = 0;
Lab_info_frame.size.height = normalheight;
[Lab_info setframe: lab_info_frame];
// Set the details height
Cgrect lab_detailinfo_frame = cgrectinset (self. Frame, 0, 0 );
Lab_detailinfo_frame.origin.x = 0;
Lab_detailinfo_frame.origin.y = normalheight;
Lab_detailinfo_frame.size.height = [self getdetaillabviewheight];
[Lab_detailinfo setframe: lab_detailinfo_frame];
//
// [Self setneedsdisplay];
}
-(Void) drawrect :( cgrect) rect
{
[Super drawrect: rect];
Self. contentview. backgroundcolor = [uicolor graycolor];
Lab_info.backgroundcolor = [uicolor clearcolor];
Lab_detailinfo.backgroundcolor = [uicolor clearcolor];
}
@ End
# Import <uikit/uikit. h>
# Import "mycell. H"
@ Interface rootviewcontroller: uitableviewcontroller
{
Nsmutablearray * isitemselected;
}
@ End
# Import "rootviewcontroller. H"
@ Implementation rootviewcontroller
# Pragma mark-
# Pragma mark view Lifecycle
-(Void) viewdidload {
[Super viewdidload];
Isitemselected = [[nsmutablearray alloc] initwithcapacity: 5];
For (INT I = 0; I <5; I ++)
{
[Isitemselected insertobject: [nsnumber numberwithbool: No] atindex: I];
}
}
# Pragma mark-
# Pragma mark table view Data Source
// Customize the number of functions in the table view.
-(Nsinteger) numberofsectionsintableview :( uitableview *) tableview {
Return 1;
}
// Customize the number of rows in the table view.
-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section {
Return 5;
}
// Customize the appearance of table View cells.
-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {
Static nsstring * cellidentifier = @ "cell ";
Mycell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier];
If (cell = nil ){
Cell = (mycell *) [[mycell alloc] initwithstyle: uitableviewcellstylesubtitle reuseidentifier: cellidentifier];
Cell. selectionstyle = uitableviewcellselectionstylenone;
}
Cell. lab_info.text = [nsstring stringwithformat: @ "title-% d", indexpath. Row];
Cell. lab_detailinfo.text = [nsstring stringwithformat: @ "subtitle-% d", indexpath. Row];
If ([[isitemselected objectatindex: indexpath. Row] boolvalue] = Yes ){
Cell. lab_detailinfo.hidden = no;
} Else {
Cell. lab_detailinfo.hidden = yes;
}
Return cell;
}
# Pragma mark-
# Pragma mark table view delegate
-(Cgfloat) tableview :( uitableview *) tableview heightforrowatindexpath :( nsindexpath *) indexpath
{
Mycell * cell = (mycell *) [self tableview: tableview cellforrowatindexpath: indexpath];
If ([[isitemselected objectatindex: indexpath. Row] boolvalue] = Yes ){
Return [Cell getmycellheight];
}
Else
{
Return cell. normalheight;
}
}
-(Void) tableview :( uitableview *) tableview didselectrowatindexpath :( nsindexpath *) indexpath {
Bool B = [[isitemselected objectatindex: indexpath. Row] boolvalue];
[Isitemselected replaceobjectatindex: indexpath. Row withobject: [nsnumber numberwithbool :! B];
//
[Tableview reloaddata];
// [Tableview reloadrowsatindexpaths: [nsarray arraywithobject: indexpath] withrowanimation: uitableviewrowanimationnone];
}
The result is as follows: