iOS 實現展開TableViewCell,下拉cell

Auditore發表於2017-12-28

這裡我自己整理了一種我認為更加便捷的方法,想法就是為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

相關文章