初始化数组:
NSDictionary *dic =@{@"Cell":@"MainCell",@"isAttached":@(NO)};
NSArray * array =@[dic,dic,dic,dic,dic,dic];
self.dataArray = [[NSMutableArrayalloc]init];
self.dataArray = [NSMutableArrayarrayWithArray:array];
对tableview数据源进行配置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[self.dataArray[indexPath.row]objectForKey:@"Cell"]isEqualToString:@"MainCell"])
{
staticNSString *CellIdentifier = @"MainCell";
TEWdocumentViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==nil) {
cell = [[TEWdocumentViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
cell.selectionStyle =UITableViewCellSelectionStyleGray;
}
cell.titleLabel.text =self.documentNames[indexPath.row];
cell.titleLabel.textColor = [UIColorblackColor];
cell.detailLabel.text =@"2015-03-06 13:43";
cell.detailLabel.textColor = [UIColorgrayColor];
cell.titleImageView.image = [UIImageimageNamed:kIconAlert];
[cell.controlButtonaddTarget:selfaction:@selector(controlButtonTapped:)forControlEvents:UIControlEventTouchUpInside];
cell.accessoryType =UITableViewCellAccessoryNone;
return cell;
}elseif([[self.dataArray[indexPath.row]objectForKey:@"Cell"]isEqualToString:@"AttachedCell"]){
staticNSString *CellIdentifier = @"AttachedCell";
TewMoreViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==nil) {
cell = [[TewMoreViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
cell.selectionStyle =UITableViewCellSelectionStyleNone;
}
return cell;
}
returnnil;
}
- (void)controlButtonTapped:(id)sender
{
NSIndexPath *path =nil;
TEWdocumentViewCell *cell = (TEWdocumentViewCell *)[[sendersuperview] superview];
NSIndexPath *indexPath = [_tableViewindexPathForCell:cell];
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.tableViewbeginUpdates];
[self.tableViewdeleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableViewendUpdates];
}else{
// 打开附加cell
NSDictionary * dic =@{@"Cell":@"MainCell",@"isAttached":@(YES)};
self.dataArray[(path.row-1)] = dic;
NSDictionary * addDic =@{@"Cell":@"AttachedCell",@"isAttached":@(YES)};
[self.dataArrayinsertObject:addDic atIndex:path.row];
[self.tableViewbeginUpdates];
[self.tableViewinsertRowsAtIndexPaths:@[path]withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableViewendUpdates];
}
}
若要求一次只显示一个附加cell的话,则在动作控制处设置
- (void)controlButtonTapped:(id)sender
{
NSIndexPath *path = nil;
TEWdocumentViewCell *cell = (TEWdocumentViewCell *)[[sender superview] superview];
NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
if ([[self.dataArray[indexPath.row] objectForKey:@"Cell"] isEqualToString:@"MainCell"]) {
path = [NSIndexPath indexPathForItem:(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.dataArray removeObjectAtIndex:path.row];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView endUpdates];
}else{
// 打开附加cell
if ([[self.dataArray[self.index.row] objectForKey:@"isAttached"] boolValue]) {
// 关闭上一个附加cell
NSDictionary * dic = @{@"Cell": @"MainCell",@"isAttached":@(NO)};
self.dataArray[(self.index.row-1)] = dic;
[self.dataArray removeObjectAtIndex:self.index.row];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[self.index] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView endUpdates];
}
if(indexPath.row > self.index.row){
path = indexPath;
}
// 打开附加cell
NSDictionary * dic = @{@"Cell": @"MainCell",@"isAttached":@(YES)};
self.dataArray[(path.row-1)] = dic;
NSDictionary * addDic = @{@"Cell": @"AttachedCell",@"isAttached":@(YES)};
[self.dataArray insertObject:addDic atIndex:path.row];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView endUpdates];
self.index = path;
}
}