點(diǎn)擊cell顯示更多信息

現(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).??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容