zoukankan      html  css  js  c++  java
  • iOS UITableView动态隐藏或显示Item

    通过改变要隐藏的item的高度实现隐藏和显示item

    1.创建UITableView

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property(nonatomic, strong)UITableView *tableView;
    @property(nonatomic, assign)BOOL isHiddenItem;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.isHiddenItem = NO;
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.view addSubview:self.tableView];
    
    }

    2.UITableView delegate, 具体的实现方法都已经加了注释

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        // 把要隐藏的item的高度设置为0
        if (indexPath.row == 2 && self.isHiddenItem) {
            return 0;
        }
        return 100;
    }
    
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 5;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        if (indexPath.row == 0) {
            cell.textLabel.text = @"点击0行的时候隐藏第2行";
        } else if(indexPath.row ==1) {
            cell.textLabel.text = @"点击1行的时候显示第2行";
    
        } else {
            cell.textLabel.text = [NSString stringWithFormat:@"当前行数%ld",indexPath.row];
        }
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row == 0) {
            // 标示是否被隐藏
            self.isHiddenItem = YES;
            
            // 获取要隐藏item的位置
            NSIndexPath *tmpPath = [NSIndexPath indexPathForRow:indexPath.row + 2 inSection:indexPath.section];
    
            [UIView animateWithDuration:0.3 animations:^{
                [self.tableView cellForRowAtIndexPath:tmpPath].alpha = 0.0f;
            } completion:^(BOOL finished) {
                // 隐藏的对应item
                [[self.tableView cellForRowAtIndexPath:tmpPath] setHidden:YES];
                // 刷新被隐藏的item
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tmpPath] withRowAnimation:UITableViewRowAnimationFade];
            }];
            NSLog(@"点击了第0行");
        } else if (indexPath.row == 1){
            
            self.isHiddenItem = NO;
            
            NSIndexPath *tmpPath = [NSIndexPath indexPathForRow:indexPath.row + 2 inSection:indexPath.section];
            
            [UIView animateWithDuration:0.3 animations:^{
                [self.tableView cellForRowAtIndexPath:tmpPath].alpha = 1.0f;
            } completion:^(BOOL finished) {
                [[self.tableView cellForRowAtIndexPath:tmpPath] setHidden:YES];
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tmpPath] withRowAnimation:UITableViewRowAnimationFade];
            }];
            NSLog(@"点击了第1行");
    
        }
    }

    3.效果

    如果你不是在wb145230博客园看到本文,请点击查看原文.

  • 相关阅读:
    行业基本面量化之商业银行
    [茶思]另类数据及其在投资中的应用
    [茶思]机器学习与宏观基本面方法在债券定价中的应用
    [宏观追踪]美国2020年2月
    [饭想]可转债市场的发展趋势
    [读论文]谷歌搜索量与个人投资者交易行为
    [读论文]基金评级及其激励
    [读论文]懒价格
    [宏观追踪]中国2020年2月
    虚拟机nat-网络认知-更正(vmware 的nat规则设置完网关,虚拟机设置自己网络就完事了。物理机相当于双网卡 物理和vmware8,8只是为了物理机xshell连接虚拟机,禁用了虚拟机照样上网)
  • 原文地址:https://www.cnblogs.com/wb145230/p/4632259.html
Copyright © 2011-2022 走看看