現(xiàn)在很多app會有點(diǎn)擊cell顯示更多信息這種情況,比如買東西,查看訂單等等,我這里寫了一個(gè)簡單的demo給大家參考一下
先看看效果圖吧,看看是不是你們想要的樣子.

代碼的思路就是每條數(shù)據(jù)是一個(gè)分區(qū),每個(gè)分區(qū)2個(gè)cell,通過判斷是展開還是收回來顯示每個(gè)分區(qū)是返回1個(gè)cell還是2個(gè)cell.思路很簡單吧,看看代碼吧.
#import "ViewController.h"
#import "InfoCell.h"
#import "MoreInfoCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray * dataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_dataSource = @[@{@"isShow":@"0"},
@{ @"isShow":@"1"},
@{@"isShow":@"0"},
].mutableCopy;
UIView *view = [[UIView alloc]initWithFrame:CGRectZero];
_tableView.tableFooterView = view;
}
#pragma mark UITableViewDataSource----UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _dataSource.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if ([[_dataSource[section] objectForKey:@"isShow"] isEqualToString:@"0"]) {
return 1;
}
return 2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 86;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 15;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
static NSString *CellIdentifier = @"infocell";
//自定義cell類
InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.zhankai.tag = 1000 + indexPath.section;
[cell.zhankai addTarget:self action:@selector(showMoreInfo:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}else{
static NSString *CellIdentifier = @"moreinfo";
//自定義cell類
MoreInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.shouqi.tag = 1000 + indexPath.section;
[cell.shouqi addTarget:self action:@selector(showMoreInfo:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
}
-(void)showMoreInfo:(UIButton *)button
{
NSInteger i = button.tag - 1000;
NSLog(@"%ld",i);
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:_dataSource[i]];
if ([[dic objectForKey:@"isShow"]isEqualToString:@"1"]) {
[dic setObject:@"0" forKey:@"isShow"];
}else
{
[dic setObject:@"1" forKey:@"isShow"];
}
_dataSource[i] = dic;
[_tableView reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade];
[_tableView reloadData];
}
好了,代碼就這么多,希望大家每天都能進(jìn)步一點(diǎn)點(diǎn).??