使用tableview的表头button 实现多 cell 的选择
首先声明本篇博文是作者原创,在QQ群里看到一枚猿友求助,问题描述:使用UItableView 实现在表头里点击不同的按钮,去刷新当前的界面(界面是大的 cell),自己就实现了一下。
实验原材料:故事版(storyBoard)、xib、UItableView、通知中心
技术点:多cell 的切换、通知的收发、*通知用完及时移除
先看效果图:
默认的当前界面是左图。点击“课程介绍”按钮就刷新为左图,点击“课程讨论”按钮就刷新为中图,点击“作品介绍”按钮就刷新为右图。

实现代码上传到我的群空间,如果需要,请去QQ群 215901818 下载。
实现关键代码:
#import "ViewController.h"
#import "CellA.h"
#import "CellB.h"
#import "CellC.h"
#import "Empty.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> @property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property(nonatomic,assign)BOOL isSelectA,isSelectB,isSelectC; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.myTableView.backgroundColor = [UIColor redColor]; //注册
[self RegisterCellID]; //通知中心去接受通知
[self getNotify]; //默认的设置,加载哪一个cell
[self setBoolCell]; } //设置,加载哪一个 cell 的 bool 值
-(void)setBoolCell{
self.isSelectA = YES;
self.isSelectB = NO;
self.isSelectC = NO;
} //接通知方法
-(void)getNotify{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeReloading:) name:nil object:nil];
} //接受到通知之后要办理的事情
-(void)changeReloading:(NSNotification *)notify{ if ([notify.userInfo[@"key"] isEqualToString:@"post1"]) {
NSLog(@"收到通知 %@",notify.userInfo[@"key"]);
self.isSelectA = YES;
self.isSelectB = NO;
self.isSelectC = NO;
[self.myTableView reloadData];
}
if ([notify.userInfo[@"key"] isEqualToString:@"post2"]) {
NSLog(@"收到通知 %@",notify.userInfo[@"key"]);
self.isSelectA = NO;
self.isSelectB = YES;
self.isSelectC = NO;
[self.myTableView reloadData];
}
if ([notify.userInfo[@"key"] isEqualToString:@"post3"]) {
NSLog(@"收到通知 %@",notify.userInfo[@"key"]);
self.isSelectA = NO;
self.isSelectB = NO;
self.isSelectC = YES;
[self.myTableView reloadData];
} } //注册cellId 方法
-(void)RegisterCellID{
[self.myTableView registerClass:[CellA class] forCellReuseIdentifier:@"cellA"];
[self.myTableView registerClass:[CellB class] forCellReuseIdentifier:@"cellB"];
[self.myTableView registerClass:[CellC class] forCellReuseIdentifier:@"cellC"];
} //得到cellID 的方法
-(NSArray *)getCellID{
NSArray * tempArr = [NSArray arrayWithObjects:@"cellA",@"cellB",@"cellC", nil];
return tempArr;
}
#pragma Mark===自定义表的头部 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
Empty * viewHead = [[[NSBundle mainBundle]loadNibNamed:@"Empty" owner:self options:nil] lastObject];
viewHead.backgroundColor = [UIColor blackColor];
viewHead.frame = CGRectMake(, , self.myTableView.bounds.size.width, );
return viewHead; }
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return ;
} #pragma Mark===必须实现的两个代理方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return ;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (self.isSelectA == YES) {
CellA * cellA = [tableView dequeueReusableCellWithIdentifier:[self getCellID][]];
if (cellA == nil) {
cellA = [[CellA alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self getCellID][]];
}
cellA.textLabel.text = @"";
return cellA; }else if (self.isSelectB == YES){ CellB * cellB = [tableView dequeueReusableCellWithIdentifier:[self getCellID][]];
if (cellB == nil) {
cellB = [[CellB alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self getCellID][]];
}
cellB.textLabel.text = @"";
return cellB; }else if (self.isSelectC == YES){ CellC * cellC = [tableView dequeueReusableCellWithIdentifier:[self getCellID][]];
if (cellC == nil) {
cellC = [[CellC alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[self getCellID][]];
}
cellC.textLabel.text = @""; return cellC; }
return nil;
} -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return ;
} //在合适的时候移除通知中心,由于 根视图不能移除,所以 dealloc 方法这里不管用,这里写在了页面消失的时候
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"post1"];
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"post2"];
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"post3"]; } @end
ViewController.m
#import "Empty.h"
@implementation Empty
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
NSLog(@"加载了噢噢噢噢");
}
return self;
}
- (IBAction)buttonA:(UIButton *)sender {
NSLog(@"点击了--课程介绍");
NSString * post1 = @"post1";
[self postNotifyWith:post1];
}
- (IBAction)buttonB:(UIButton *)sender {
NSLog(@"点击了--课程讨论");
NSString * post1 = @"post2";
[self postNotifyWith:post1];
}
- (IBAction)buttonC:(UIButton *)sender {
NSLog(@"点击了--作品展示");
NSString * post1 = @"post3";
[self postNotifyWith:post1];
}
//发通知实现
-(void)postNotifyWith:(NSString *)sender{
[[NSNotificationCenter defaultCenter] postNotificationName:sender object:self userInfo:@{@"key":sender}];
}
@end
Empty.m
对应的故事版如下左图、对应的xib如下右图所示:

可使用领域的猜想:作为页面的加载少量数据的组件出现
有可能出现的问题:效率底下、在通知中心的使用上有可能出现问题
other(再小的组件功能实现了就是一件开心的事情)
使用tableview的表头button 实现多 cell 的选择的更多相关文章
- 防止TableView 上的tap手势隔断 cell的选择
遵循UIGestureRecognizerDelegate协议: 1.0添加手势 - (void)addTapGest { UITapGestureRecognizer *tap = [[U ...
- iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang
转载请注明出处. 今天在调试代码的时候,在tableviewcell上添加button,发现button快速点击的话,是看不出点击效果的,查找资料发现, ios7上UITableViewCell子层容 ...
- ios 获取button所在的cell对象, 注意:ios7 =< System Version < ios8 获取cell对象的差别
ios7 =< System Version< ios8 : ios7 =< System Version < ios8 下 button.superview.supervi ...
- TableView之表头、表尾,区头、区尾!
一.UITableView的UITableViewStyle 样式分为UITableViewStylePlain和UITableViewStyleGrouped两种: plain样式下区头和区尾是悬浮 ...
- 点击某个按钮在tableView某个位置动态插入一行cell
实现步骤: 1.修改数据模型数组 给模型数组的某个位置增加一个模型 2.执行以下代码 NSIndexPath *indexPath = [NSIndexPath indexPathForRow: in ...
- TableView不显示没内容的Cell怎么办?
类似这种,我不想让下面那些空的显示. 很简单: self.tableView.tableFooterView = [[UIView alloc] init]; 加完这句之后就变成了这样:
- iOS学习——tableview中带编辑功能的cell键盘弹出遮挡和收起问题解决
最近在项目中经常用到UITableView中的cell中带有UITextField或UITextView的情况,然后在这种场景下,当我们点击屏幕较下方的cell进行编辑时,这时候键盘弹出来会出现遮挡待 ...
- 实现uitable cell中点击button设置当前cell为选中状态
- (void)buttonClick:(id)senser{ NSInteger tag = [senser tag]; NSLog(@"the button tag is % ...
- IOS 取消表格单元格 TableViewCell 去掉高亮状态 点击Cell取消选择状态
以下是两种实现效果 1. 自定义cell 继承UITableViewCell 重写 -(void)setSelected:(BOOL)selected animated:(BOOL)animated ...
随机推荐
- mvc用UpdateModel报错
项目中使用UpdateModel时报错:未能更新类型“XXXXXX”的模型. 原因如下:表单Post时,有的参数为空,如a=1&b=2&=3.
- 编码知识 (Unicode、UTF-8、ANSI)
1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte). ...
- MySQL windows集群(转)
http://blog.csdn.net/zhangking/article/details/5670070 MySQL 群集是 MySQL 适合于分布式计算环境的高可用.高冗余版本.它采用了 ...
- tensor搭建--windows 10 64bit下安装Tensorflow+Keras+VS2015+CUDA8.0 GPU加速
windows 10 64bit下安装Tensorflow+Keras+VS2015+CUDA8.0 GPU加速 原文见于:http://www.jianshu.com/p/c245d46d43f0 ...
- ORACLE 查看表结构
select table_name from user_tables; //当前用户的表 select table_name from all_tables; //所有用户的表 select tabl ...
- C#基础系列:反射笔记
前言:使用反射也有几年了,但是一直觉得,反这个概念很抽象,今天有时间就来总结下这个知识点. 1.为什么需要反射: 最初使用反射的时候,作为小菜总是不理解,既然可以通过new 一个对象的方式得到对象,然 ...
- iframe仿ajax图片上传
1.前台页面: iframe_upload.html <html> <body> <form action="upload.php" id=" ...
- 【BZOJ1064】[Noi2008]假面舞会 DFS树
[BZOJ1064][Noi2008]假面舞会 Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择 ...
- Elasticsearch + Logstash + Kibana 搭建教程
# ELK:Elasticsearch + Logstash + Kibana 搭建教程 Shipper:日志收集者.负责监控本地日志文件的变化,及时把日志文件的最新内容收集起来,输出到Redis暂存 ...
- Boosting AdaBoosting Algorithm
http://math.mit.edu/~rothvoss/18.304.3PM/Presentations/1-Eric-Boosting304FinalRpdf.pdf Consider MIT ...