Cell expansion in several ways

Source: Internet
Author: User

I. Inserting a new cell

Principle:

(1) Define whether to expand, and the subscript of the expanded cell

// whether to expand the @property (strong, nonatomic) Nsindexpath *selectedindexpath; // The subscript for the expanded cell

(2) Create two different cells

 

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {UITableViewCell*cell; if(Self.isexpand && Self.selectedIndexPath.row < indexpath.row && Indexpath.row <= Self.selectedIndexPath.row + expandcount) {//Expand Cellcell = [TableView dequeuereusablecellwithidentifier:@"Csutomexpansioncell"Forindexpath:indexpath]; } Else{//Normal Cellcell = [TableView dequeuereusablecellwithidentifier:@"Customcell"Forindexpath:indexpath]; }    returncell;}

(3) Create the number of cells you need

if (Self.isexpand) {        return cellcount + expandcount;    }     return Cellcount;

(4) Click on the cell below to insert the cell you need to show (can expand multiple), click Delete again

- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {if(!self.selectedindexpath) {Self.isexpand=YES; Self.selectedindexpath=Indexpath;        [Self.tavleview beginupdates]; [Self.tavleview insertrowsatindexpaths:[self IndexPathsForExpandRow:indexPath.row] Withrowanimation:        Uitableviewrowanimationtop];    [Self.tavleview endupdates]; } Else {        if(Self.isexpand) {if(Self.selectedindexpath = =Indexpath) {Self.isexpand=NO;                [Self.tavleview beginupdates]; [Self.tavleview deleterowsatindexpaths:[self IndexPathsForExpandRow:indexPath.row] Withrowanimation:                Uitableviewrowanimationtop];                [Self.tavleview endupdates]; Self.selectedindexpath=Nil; } Else if(Self.selectedIndexPath.row < Indexpath.row && Indexpath.row <= Self.selectedIndexPath.row +Expandcount) {                            } Else{Self.isexpand=NO;                [Self.tavleview beginupdates]; [Self.tavleview deleterowsatindexpaths:[self IndexPathsForExpandRow:self.selectedIndexPath.row] Withrowanimation:                Uitableviewrowanimationtop];                [Self.tavleview endupdates]; Self.selectedindexpath=Nil; }        }    }}#pragmamark-other-(Nsarray *) Indexpathsforexpandrow: (Nsinteger) row {Nsmutablearray*indexpaths =[Nsmutablearray array];  for(inti =1; I <= Expandcount; i++) {Nsindexpath*idxpth = [Nsindexpath Indexpathforrow:row + i insection:0];    [Indexpaths addobject:idxpth]; }    return[indexpaths copy];}

Two. Inserting a cell into a different section

Principle:

(1) Define whether to expand, and the subscript of the expanded cell

(2) Create two different cells

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {UITableViewCell*cell; if(Self.isexpand && self.selectedIndexPath.section = = indexpath.section) {//Expand Cellcell = [TableView dequeuereusablecellwithidentifier:@"Csutomexpansioncell"Forindexpath:indexpath]; } Else{//Normal Cellcell = [TableView dequeuereusablecellwithidentifier:@"Customcell"Forindexpath:indexpath]; }        returncell;}

(3) Create the number of cell,section you need to show in normal state

-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableView {    return  sectioncount;}

(4) Change the number of cells that expand the section when you expand

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {    if (self.isexpand && self.selectedIndexPath.section = = Section )        {return1 // Multiple Quantities     }    return1;}

(5) Click on the cell's section to insert the cell you need to show (can expand multiple), click Delete again

- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {if(!self.selectedindexpath) {Self.isexpand=YES; Self.selectedindexpath=Indexpath;        [Self.tableview beginupdates]; [Self.tableview insertrowsatindexpaths:[self indexPathsForExpandSection:indexPath.section] Withrowanimation:        Uitableviewrowanimationtop];    [Self.tableview endupdates]; } Else {        if(Self.isexpand) {if(Self.selectedindexpath = =Indexpath) {Self.isexpand=NO;                [Self.tableview beginupdates]; [Self.tableview deleterowsatindexpaths:[self indexPathsForExpandSection:indexPath.section] Withrowanimation:                Uitableviewrowanimationtop];                [Self.tableview endupdates]; Self.selectedindexpath=Nil; } Else if(Self.selectedIndexPath.row! = Indexpath.row && indexpath.section <=self.selectedIndexPath.section) {//Select The Expand Cell, do the relating dealing.}Else{Self.isexpand=NO;                [Self.tableview beginupdates]; [Self.tableview deleterowsatindexpaths:[self IndexPathsForExpandSection:self.selectedIndexPath.section]                Withrowanimation:uitableviewrowanimationtop];                [Self.tableview endupdates]; Self.selectedindexpath=Nil; }        }    }}-(Nsarray *) Indexpathsforexpandsection: (nsinteger) Section {Nsmutablearray*indexpaths =[Nsmutablearray array];  for(inti =1; I <= Expandcount; i++) {Nsindexpath*idxpth =[Nsindexpath indexpathforrow:i insection:section];    [Indexpaths addobject:idxpth]; }    return[indexpaths copy];}

Three. Change the cell's height

Principle:

(1) Define whether to expand, and the subscript of the expanded cell

(2) Create a cell, split the upper and lower halves

(3) The height of the cell is created, the height of the normal case and the height after expansion

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath {      If (self.isexpand && Self.selectedindexpath = = Indexpath)        {return121 ;     Else {        return;    }}

(4) Click on the cell to refresh the clicked cell

- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {if(!self.selectedindexpath) {Self.isexpand=YES; Self.selectedindexpath=Indexpath;        [Self.tableview beginupdates];        [Self.tableview Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade];    [Self.tableview endupdates]; } Else {        if(Self.isexpand) {if(Self.selectedindexpath = =Indexpath) {Self.isexpand=NO;                [Self.tableview beginupdates];                [Self.tableview Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade];                [Self.tableview endupdates]; Self.selectedindexpath=Nil; } Else{Self.isexpand=NO;                [Self.tableview beginupdates];                [Self.tableview Reloadrowsatindexpaths:@[self.selectedindexpath] withrowanimation:uitableviewrowanimationfade];                [Self.tableview endupdates]; Self.selectedindexpath=Nil; }        }    }}

Four. Customize section, click to expand the appropriate cell (write in the afternoon ...)

Demo Link

Http://pan.baidu.com/s/1c0YQDNE

Cell expansion in several ways

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.