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 == && 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的更多相关文章
- 关于iOS导航控制器隐藏和显示会出现返回键失效,导航栏标题动画异常
最近做的demo bug出现了,我觉得这个bug出现得很经典所以贴出来给大家看看, bug演示就是:点击返回键失效出现如下gif图演示的内容 为啥会出现如此奇葩的bug,系统的返回键居然失效了,尴尬 ...
- JQuery动态隐藏和显示DIV
<head> <script language="javascript"> function HideWeekMonth() { $("#tt1& ...
- react中如何实现一个按钮的动态隐藏和显示(有效和失效)
初始准备工作 constructor(props) { super(props); /* * 构建导出数据的初始参数,结合用户下拉选择后动态设置参数值 * */ this.state = { btnS ...
- datagrid其中某列需要动态隐藏或显示的mvvm绑定方式,也可以用在其他表格类型控件上
版权归原作者所有. 引用地址 [WPF] HOW TO BIND TO DATA WHEN THE DATACONTEXT IS NOT INHERITED MARCH 21, 2011 THOMAS ...
- iOS UITableView表视图滚动隐藏UINavigationController导航栏
UITableView 继承于UIScrollView 所以UIScrollView 的代理方法相同适用于UITableView 中 隐藏导航栏的方法为: self.navigationControl ...
- iOS UITableView 与 UITableViewController
很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名.项目地址. UITab ...
- iOS UITableView优化
一.Cell 复用 在可见的页面会重复绘制页面,每次刷新显示都会去创建新的 Cell,非常耗费性能. 解决方案:创建一个静态变量 reuseID,防止重复创建(提高性能),使用系统的缓存池功能. s ...
- React-Native 之 GD (四)使用通知方式隐藏或显示TabBar
1.GDHalfHourHot.js 发送通知 /** * 近半小时热门 */ import React, { Component } from 'react'; import { StyleShe ...
- IOS 公共类-MyMBProgressUtil Progress显示
IOS 公共类-MyMBProgressUtil Progress显示 此公共类用于显示提示框,对MBProgress的进一步封装.可以看下面的代码 接口: @interface MyMBProgre ...
随机推荐
- poj 2965 The Pilots Brothers' refrigerator
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18040 ...
- php中的foreach如何使用?
php中的foreach如何使用? 一.总结 1.foreach (array_expression as $value) 2.foreach (array_expression as $key =& ...
- C++常用数据结构的实现
常用数据结构与算法的实现.整理与总结 我将我所有数据结构的实现放在了github中:Data-Structures-Implemented-By-Me 常用数据结构与算法的实现.整理与总结 KMP字符 ...
- erlang中变量作用域
http://erlangdisplay.iteye.com/blog/315452 _开头(包括_)在erlang可以是表明,这个变量可以存任意东西,就是我们常说的全匹配,_A一般来说就是表明这个东 ...
- C#反射应用
考虑这个是因为返回的是对象集合,需要把对象集合绑定到datagridview上,绑定datagridview需要数据源,组装数据的话,用datatable添加列很麻烦,所以用反射来实现,估计可能会有多 ...
- 2015-07-30Java 错题
2 推断对错.在java的多态调用中,new的是哪一个类就是调用的哪个类的方法. 正确答案: A 对 错 解析: java多态有两种情况:重载和覆写 在覆写中.运用的是动态单分配.是依据new的类型确 ...
- ART、JIT、AOT、Dalvik之间有什么关系?
JIT与Dalvik JIT是"Just In Time Compiler"的缩写,就是"即时编译技术",与Dalvik虚拟机相关. 怎么理解这句话呢?这要从A ...
- 卷积神经网络(CNN)与特殊的卷积
各种卷积操作的可视化的显示形式:GitHub - vdumoulin/conv_arithmetic: A technical report on convolution arithmetic in ...
- 手机预览微信小程序
1. 获取微信小程序的 AppID 登录 https://mp.weixin.qq.com ,就可以在网站的“设置”-“开发者设置”中,查看到微信小程序的 AppID 了,注意不可直接使用服务号或订阅 ...
- solr 7.x 查询及高亮
查询时的api分为两种一种是万能的set,还有一种是setxxxquery @Test public void search2() throws Exception{ HttpSolrClient s ...