现在很多app会有点击cell显示更多信息这种情况,比如买东西,查看订单等等,我这里写了一个简单的demo给大家参考一下
先看看效果图吧,看看是不是你们想要的样子.
代码的思路就是每条数据是一个分区,每个分区2个cell,通过判断是展开还是收回来显示每个分区是返回1个cell还是2个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];
}
好了,代码就这么多,希望大家每天都能进步一点点.😄