iOS 实现展开TableViewCell,下拉cell

486 阅读2分钟

这里我自己整理了一种我认为更加便捷的方法,想法就是为cell设置一个标识符,isSpread,展开或者未展开,cell本身设定高度为200,未展开的时候高度设为100,通过改变标识符,再去刷新cell的高度就能达到展开和收起cell的效果

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

//单元格展开功能20160606

ArewDataModle*modle =self.sectionDataArr3[indexPath.row];

if(modle.isSpread==NO) {

return55;

}else{

return100;

}

}

//CellForRow里的关键代码

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

cell.spreadImage.hidden=NO;

if(self.sectionDataArr3[indexPath.row].isSpread==NO)

{

}else{

cell.additionalLabel.hidden=NO;

}

UITapGestureRecognizer*tapSpreadImage = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(SpreadImageClick:)];

//传递当前手势

tapSpreadImage.delegate=self;

[cell.spreadImageaddGestureRecognizer:tapSpreadImage];

}

- (void)SpreadImageClick:(UITapGestureRecognizer*)tap{

//传递当前手势,通过手势识别出点击的cell

ArewTableViewCell* cell = (ArewTableViewCell*)tap.view.superview.superview;

NSIndexPath*indexpath = [self.brewTimeTableindexPathForCell:cell];

//单元格展开功能

ArewDataModle*modle =self.sectionDataArr3[indexpath.row];

modle.isSpread= !modle.isSpread;

cell.additionalLabel.hidden= !self.isSpreadImage;

[self.ArewTimeTablereloadRowsAtIndexPaths:[NSArrayarrayWithObjects:indexpath,nil]withRowAnimation:UITableViewRowAnimationNone];

}



还有一种网络上的 单击一个cell的时候 在当前cell下在添加一个自己定义好的Cell,重点是处理数据源数组

本代码网上整理出来的。

@property(weak,nonatomic)IBOutletUITableView*my_tableView;

@property(nonatomic,strong)NSMutableArray*dataArray;

@property(assign)BOOLisOpen;

@end

@implementationMoveCell

- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil

{

self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

if(self) {

// Custom initialization

}

returnself;

}

- (void)viewDidLoad

{

[superviewDidLoad];

[selfsetTitle:@"我的cell"];

[selfsetLeftButtonText:@""andBackground:[UIImageimageNamed:@"btn_back"]];

NSDictionary*dic =@{@"Cell":@"MainCell",@"isAttached":@(NO)};

NSArray* array =@[dic,dic,dic,dic,dic,dic];

self.dataArray= [[NSMutableArrayalloc]init];

self.dataArray= [NSMutableArrayarrayWithArray:array];

}

- (NSInteger)tableView:(UITableView*)tableView

numberOfRowsInSection:(NSInteger)section

{

// Return the number of rows in the

section.

returnself.dataArray.count;;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

{

// Return the number of sections.

return1;

}

// tableViewCell

-(UITableViewCell*)tableView:(UITableView*)tableView

cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

if([[self.dataArray[indexPath.row]objectForKey:@"Cell"]isEqualToString:@"MainCell"])

{

staticNSString*CellIdentifier =@"MainCell";

DDIUICtrl_messageCell*cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

if(cell ==nil) {

cell = [[DDIUICtrl_messageCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

cell.selectionStyle=UITableViewCellSelectionStyleGray;

}

//

cell.Headerphoto.image = [UIImage imageNamed:[NSString

stringWithFormat:@"%d.jpg",indexPath.row%4+1]];

returncell;

}elseif([[self.dataArray[indexPath.row]objectForKey:@"Cell"]isEqualToString:@"AttachedCell"]){

staticNSString*CellIdentifier =@"AttachedCell";

DDUICtrl_menuCell*cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

if(cell ==nil) {

cell = [[DDUICtrl_menuCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

cell.selectionStyle=UITableViewCellSelectionStyleNone;

}

returncell;

}

returnnil;

}

// tableView点击事件

-(void)tableView:(UITableView*)tableView

didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

[tableViewdeselectRowAtIndexPath:indexPathanimated:YES];

NSIndexPath*path =nil;

if([[self.dataArray[indexPath.row]objectForKey:@"Cell"]isEqualToString:@"MainCell"]) {

path = [NSIndexPathindexPathForItem:(indexPath.row+1)inSection:indexPath.section];

}else{

path

= indexPath;

}

if([[self.dataArray[indexPath.row]objectForKey:@"isAttached"]boolValue]) {

//关闭附加cell

NSDictionary* dic =@{@"Cell":@"MainCell",@"isAttached":@(NO)};

self.dataArray[(path.row-1)] = dic;

[self.dataArrayremoveObjectAtIndex:path.row];

[self.my_tableViewbeginUpdates];

[self.my_tableViewdeleteRowsAtIndexPaths:@[path]withRowAnimation:UITableViewRowAnimationMiddle];

[self.my_tableViewendUpdates];

}else{

//打开附加cell

NSDictionary* dic =@{@"Cell":@"MainCell",@"isAttached":@(YES)};

self.dataArray[(path.row-1)] = dic;

NSDictionary* addDic =@{@"Cell":@"AttachedCell",@"isAttached":@(YES)};

[self.dataArrayinsertObject:addDicatIndex:path.row];

[self.my_tableViewbeginUpdates];

[self.my_tableViewinsertRowsAtIndexPaths:@[path]withRowAnimation:UITableViewRowAnimationMiddle];

[self.my_tableViewendUpdates];

}

}

@end