Expansion contraction of IOS tableview cell

Source: Internet
Author: User

 Expansion contraction of IOS tableview cell 

#import "ViewController.h"

@interface Viewcontroller () <uitableviewdatasource,uitableviewdelegate>{

Nsmutablearray *_allarray;//Creating an array of data sources

Nsmutabledictionary *dic;//Create a dictionary to decide whether to shrink or expand

}

@property (Nonatomic,strong) UITableView *tableview;

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Self.view.backgroundColor = [Uicolor Whitecolor];

DIC = [Nsmutabledictionary dictionary];

_allarray = [@[@[@ "12", @ "32", @ "12", @ "12", @ "12", @ "32"],@[@ "12", @ "12", @ "12", @ "32", @ "12", @ "12", @ "12", @ "32", @ "12", @ " "@", @ "a", @ "],@[@", @ "a", @ "+"]]mutablecopy];

[Self.view AddSubview:self.tableView];

}

Lazy Loading

-(UITableView *) tableview{

if (!_tableview) {

_tableview = [[Uitableviewalloc]initwithframe:cgrectmake (0, 375, 667-64) Style:uitableviewstyleplain];

_tableview.delegate = self;

_tableview.datasource = self;

}

return _tableview;

}

-(Nsinteger) Numberofsectionsintableview: (uitableview*) tableview{

return _allarray.count;

}

-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (Nsinteger) section{

return 30;

}

//

-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (Nsinteger) section{

UIView *view = [UIView new];

View.backgroundcolor = [Uicolor Redcolor];

Create a gesture to click, this can be changed to a button

UITapGestureRecognizer *tap = [[Uitapgesturerecognizeralloc]initwithtarget:self Action: @selector (Action_tap:)];

View.tag = + Section;

[View Addgesturerecognizer:tap];

return view;

}

-(void) Action_tap: (Uigesturerecognizer *) tap{

NSString *str = [nsstringstringwithformat:@ "%ld", tap.view.tag-300];

if ([Dic[str] integervalue] = = 0) {//If it is 0, assign 1 to the dictionary and open the cell

[DiC setobject:@ "1" forkey:str];

}else{//Turn off cell instead

[dic setobject:@ "0" forkey:str];

}

[Self.tableview Reloaddata];

[Self.tableview reloadsections:[nsindexset indexsetwithindex:[str integervalue]]withrowanimation: uitableviewrowanimationfade];//Animated Refresh

}

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{

NSString *string = [nsstringstringwithformat:@ "%ld", section];

if ([dic[string] integervalue] = = 1) {//Open cell returns the count of the array

Nsarray *array = [nsarrayarraywitharray:_allarray[section]];

return array.count;

}else{

return 0;

}

}

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{

Return 35;

}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{

UITableViewCell *cell = [tableviewdequeuereusablecellwithidentifier:@ "cell"];

if (!cell) {

cell = [[uitableviewcellalloc]initwithstyle:uitableviewcellstylevalue1reuseidentifier:@ ' cell '];

}

Cell.backgroundcolor = [Uicolor Orangecolor];

Cell.textLabel.text = _allarray[indexpath.section][indexpath.row];

return cell;

}

@end

Expansion contraction of IOS tableview cell

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.