使用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 ...
随机推荐
- Linux kernel Wikipedia
http://en.wikipedia.org/wiki/Linux_kernel Development model The current development model of the Lin ...
- laravel 将数组转化成字符串 再把字符串转化成数组
这是在给阮少翔改代码的时候用的方法, 开始的数据用explored转化成数组不是想要的结果, 我就自己写了一个方法把有用的信息提取出来拼接成一个字符串, 再用explored将字符串转化成数组. ...
- Ubuntu16.04 下docker部署web项目
概念性的请戳 第一步:更新apt-get update 第二步:安装环境 apt-get install \ apt-transport-https \ ca-certificates \ curl ...
- Canvas学习笔记——动画中摩擦力的运用
摩擦力是与物体运动方向相反的力.我们在处理物体运动时,常把物体分解水平(X轴)方向和竖直(Y轴)方向的运动(比如平抛运动),但在处理摩擦力时,如果把摩擦力分解为X轴和Y轴上的阻力,就会出现某条轴上速度 ...
- TXT文本写入数据库
load data local infile "D:/abc.txt" into table lee; leedabao.txt内容如下,中间用Tab隔开: 2 yuanpeng ...
- CCNET自动构建之路
人永远追求效率(想偷懒),不想手动编译项目.发布站点于是产生了自动构建技术,.NET领域中CCNET是个不错的选择. 一路问题不少,记录一下. 准备环境 服务器上需要有iis.vs(与开发环境的版本一 ...
- poj1125--Floyd
题解: 有N个股票经济人能够互相传递消息.他们之间存在一些单向的通信路径.如今有一个消息要由某个人開始传递给其它全部人.问应该由哪一个人来传递,才干在最短时间内让全部人都接收到消息. 显然,用Floy ...
- NS3网络仿真(11): ARP
快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 ARP(Address ResolutionProtocol,地址解析协议)协议的基本功能就是通过 ...
- 【BZOJ3197】[Sdoi2013]assassin 树同构+动态规划+KM
[BZOJ3197][Sdoi2013]assassin Description Input Output Sample Input 4 1 2 2 3 3 4 0 0 1 1 1 0 0 0 Sam ...
- EasyHLS实现将IPCamera摄像机的RTSP流转成HLS(ts+m3u8)直播输出
本文转自:http://www.cnblogs.com/babosa/p/6033039.html EasyHLS EasyHLS是EasyDarwin开源流媒体团队开发的一款HLS打包库,接口非常简 ...