Uitableview folding Effect

Source: Internet
Author: User

1: create a model data model

# Import <Foundation/Foundation. h>

 

@ Interface datamodel: nsobject

// Save the data of each row in the Section

@ Property (nonatomic, strong) nsmutablearray * array;

// Section name

@ Property (nonatomic, copy) nsstring * Name;

// Determine whether the section is expanded

@ Property (nonatomic, assign) bool isexpand;

@ End

 

# Import "datamodel. H"

 

@ Implementation datamodel

-(ID) Init

{

If (Self = [Super init]) {

_ Array = [nsmutablearray array];

}

Return self;

}

@ End

 

 

2: Create a viewcontroller

# Import <uikit/uikit. h>

 

@ Interface viewcontroller: uiviewcontroller

@ End

 

# Import "viewcontroller. H"

# Import "datamodel. H"

@ Interface viewcontroller () <uitableviewdelegate, uitableviewdatasource>

@ Property (weak, nonatomic) iboutlet uitableview * mytableview;

@ Property (nonatomic, strong) nsmutablearray * dataarray;

@ End

 

@ Implementation viewcontroller

 

-(Void) viewdidload

{

[Super viewdidload];

_ Dataarray = [nsmutablearray array];

For (INT I = 'a'; I <= 'D'; I ++)

{

// Set the group name

Datamodel * model = [[datamodel alloc] init];

Model. Name = [nsstring stringwithformat: @ "% C", I];

For (Int J = 0; j <5; j ++ ){

// Set rows in each group

[Model. array addobject: [nsstring stringwithformat: @ "% C-% d", I, j];

}

[_ Dataarray addobject: Model];

}

--- PS: I have attached a line on the storyboard ---

// _ Mytableview. datasource = self;

// _ Mytableview. Delegate = self;

}

 

-(Void) didreceivememorywarning

{

[Super didreceivememorywarning];

// Dispose of any resources that can be recreated.

}

# Pragma mark-uitableviewdatasource

# Pragma mark-uitableviewdatasource

// Number of returned groups

-(Nsinteger) numberofsectionsintableview :( uitableview *) tableview

{

Return _ dataarray. count;

}

// Returns the number of rows in each group.

-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section

{

Datamodel * model = _ dataarray [section];

If (model. isexpand)

{

Return Model. array. count;

}

Else

{

Return 0;

}

}

// The value to be displayed in each row

-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath

{

Static nsstring * [email protected] "cell ";

Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier];

If (cell = nil ){

Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier];

}

Datamodel * model = _ dataarray [indexpath. Section];

Cell. textlabel. Text = model. array [indexpath. Row];

Cell. accessorytype = uitableviewcellaccessorydisclosureindicator;

Return cell;

}

// Add a button to the group name

-(Uiview *) tableview :( uitableview *) tableview viewforheaderinsection :( nsinteger) Section

{

// Add a view storage button

Uiview * contentview = [[uiview alloc] initwithframe: cgrectmake (0, 0,320, 32)];

Datamodel * model = _ dataarray [section];

Uibutton * BTN = [uibutton buttonwithtype: uibuttontypecustom];

BTN. Frame = cgrectmake (375-15, 5, 15, 8 );

BTN. Tag = Section;

[BTN addtarget: Self action: @ selector (btnclick :) forcontrolevents: uicontroleventtouchupinside];

If (model. isexpand)

{

[BTN setbackgroundimage: [uiimage imagenamed: @ "open"] forstate: uicontrolstatenormal];

}

Else

{

[BTN setbackgroundimage: [uiimage imagenamed: @ "close"] forstate: uicontrolstatenormal];

}

[Contentview addsubview: BTN];

If (section % 2 = 0)

{

Contentview. backgroundcolor = [uicolor colorwithred: arc4random () % 256/255. 0 Green: arc4random () % 256/255. 0 Blue: arc4random () % 256/255. 0 ALPHA: 1.0];

}

Else

{

Contentview. backgroundcolor = [uicolor colorwithred: arc4random () % 256/255. 0 Green: arc4random () % 256/255. 0 Blue: arc4random () % 256/255. 0 ALPHA: 1.0];

}

Return contentview;

}

 

// Use the button on the group name to determine whether to expand

-(Void) btnclick :( uibutton *) sender

{

Datamodel * Data = _ dataarray [Sender. Tag];

If (data. isexpand)

{

Data. isexpand = no;

}

Else

{

Data. isexpand = yes;

}

[_ Mytableview reloaddata];

}

 

 

@ End

 

 

3: Test

 

Uitableview folding Effect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.