通过改变要隐藏的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 == && self.isHiddenItem) {
return ;
}
return ;
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ;
} -(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 == ) {
cell.textLabel.text = @"点击0行的时候隐藏第2行";
} else if(indexPath.row ==) {
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 == ) {
// 标示是否被隐藏
self.isHiddenItem = YES; // 获取要隐藏item的位置
NSIndexPath *tmpPath = [NSIndexPath indexPathForRow:indexPath.row + 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 == ){ self.isHiddenItem = NO; NSIndexPath *tmpPath = [NSIndexPath indexPathForRow:indexPath.row + 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博客园看到本文,请点击查看原文.

iOS UITableView动态隐藏或显示Item的更多相关文章

  1. 关于iOS导航控制器隐藏和显示会出现返回键失效,导航栏标题动画异常

    最近做的demo  bug出现了,我觉得这个bug出现得很经典所以贴出来给大家看看, bug演示就是:点击返回键失效出现如下gif图演示的内容 为啥会出现如此奇葩的bug,系统的返回键居然失效了,尴尬 ...

  2. JQuery动态隐藏和显示DIV

    <head> <script language="javascript"> function HideWeekMonth() { $("#tt1& ...

  3. react中如何实现一个按钮的动态隐藏和显示(有效和失效)

    初始准备工作 constructor(props) { super(props); /* * 构建导出数据的初始参数,结合用户下拉选择后动态设置参数值 * */ this.state = { btnS ...

  4. datagrid其中某列需要动态隐藏或显示的mvvm绑定方式,也可以用在其他表格类型控件上

    版权归原作者所有. 引用地址 [WPF] HOW TO BIND TO DATA WHEN THE DATACONTEXT IS NOT INHERITED MARCH 21, 2011 THOMAS ...

  5. iOS UITableView表视图滚动隐藏UINavigationController导航栏

    UITableView 继承于UIScrollView 所以UIScrollView 的代理方法相同适用于UITableView 中 隐藏导航栏的方法为: self.navigationControl ...

  6. iOS UITableView 与 UITableViewController

    很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名.项目地址. UITab ...

  7. iOS UITableView优化

    一.Cell 复用 在可见的页面会重复绘制页面,每次刷新显示都会去创建新的 Cell,非常耗费性能.  解决方案:创建一个静态变量 reuseID,防止重复创建(提高性能),使用系统的缓存池功能. s ...

  8. React-Native 之 GD (四)使用通知方式隐藏或显示TabBar

    1.GDHalfHourHot.js  发送通知 /** * 近半小时热门 */ import React, { Component } from 'react'; import { StyleShe ...

  9. IOS 公共类-MyMBProgressUtil Progress显示

    IOS 公共类-MyMBProgressUtil Progress显示 此公共类用于显示提示框,对MBProgress的进一步封装.可以看下面的代码 接口: @interface MyMBProgre ...

随机推荐

  1. [CSS Flex] Justify-content

    justify content contol how element inside flex box align, it can be "right / end", or &quo ...

  2. 强大的 function adapters

    void printElem(int elem, const char* prefix){ cout << prefix << elem << endl; } fo ...

  3. Linux中mv重命名作用及打包war压缩文件及分配权限

    1.Linux中的重命名文件使用mv命令 touch a.txt 新建一个文件 mv a.txt b.txt 重命名文件为b.txt mkdir abc 新建一个目录 mv abc abd 重命名文件 ...

  4. WPF入门(三)->几何图形之椭圆形(EllipseGeometry)

    原文:WPF入门(三)->几何图形之椭圆形(EllipseGeometry) 我们可以使用EllipseGeometry 来绘制一个椭圆或者圆形的图形 EllipseGeometry类: 表示圆 ...

  5. PHPDocumentor 整理目光规范

    你会写凝视么?从我写代码開始.这个问题就一直困扰着我.相信也相同困扰着其它同学.曾经的写凝视总是没有一套行之有效的标准,给维护和协同开发带了很多麻烦,直到近期读到了phpdocumentor的凝视标准 ...

  6. spring boot打包后在tomcat无法访问静态资源问题

    我的spring boot项目中前端页面的资源引用 我的静态文件夹是 我的application.yml中资源路径配置了 同时我在WebMvcConfig中配置了addResourceHandlers ...

  7. 华为云软件开发云:容器DevOps,原来如此简单!

    当开发团队把代码提交到 Git 应用仓库的那一刻,他们心里在想什么? 祈祷没有bug?渴望回家补觉?产品经理Go Die? 对,也不对.因为这只是最终发布万里长征的一小步,接下来要面对测试环境.生产环 ...

  8. matlab 图像分块及恢复

    1. block_divide % 返回的块向量构成的矩阵,其维度信息为 K^2 * N,每一列由块构成的列向量 function P = block_divide(I, K) r = size(I, ...

  9. mac下装php5.6

    OS X10.11自带了php5.5,项目中使用的是php5.6,用brew install --without-apache --with-fpm --with-mysql php56装php5.6 ...

  10. Computer system with dual operating modes

    A system switches between non-secure and secure modes by making processes, applications, and data fo ...