首先声明本篇博文是作者原创,在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 的选择的更多相关文章

  1. 防止TableView 上的tap手势隔断 cell的选择

    遵循UIGestureRecognizerDelegate协议: 1.0添加手势   - (void)addTapGest {    UITapGestureRecognizer *tap = [[U ...

  2. iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang

    转载请注明出处. 今天在调试代码的时候,在tableviewcell上添加button,发现button快速点击的话,是看不出点击效果的,查找资料发现, ios7上UITableViewCell子层容 ...

  3. ios 获取button所在的cell对象, 注意:ios7 =&lt; System Version &lt; ios8 获取cell对象的差别

    ios7 =< System Version< ios8 : ios7 =< System Version < ios8  下 button.superview.supervi ...

  4. TableView之表头、表尾,区头、区尾!

    一.UITableView的UITableViewStyle 样式分为UITableViewStylePlain和UITableViewStyleGrouped两种: plain样式下区头和区尾是悬浮 ...

  5. 点击某个按钮在tableView某个位置动态插入一行cell

    实现步骤: 1.修改数据模型数组 给模型数组的某个位置增加一个模型 2.执行以下代码 NSIndexPath *indexPath = [NSIndexPath indexPathForRow: in ...

  6. TableView不显示没内容的Cell怎么办?

    类似这种,我不想让下面那些空的显示. 很简单: self.tableView.tableFooterView = [[UIView alloc] init]; 加完这句之后就变成了这样:

  7. iOS学习——tableview中带编辑功能的cell键盘弹出遮挡和收起问题解决

    最近在项目中经常用到UITableView中的cell中带有UITextField或UITextView的情况,然后在这种场景下,当我们点击屏幕较下方的cell进行编辑时,这时候键盘弹出来会出现遮挡待 ...

  8. 实现uitable cell中点击button设置当前cell为选中状态

    - (void)buttonClick:(id)senser{    NSInteger tag = [senser tag];    NSLog(@"the button tag is % ...

  9. IOS 取消表格单元格 TableViewCell 去掉高亮状态 点击Cell取消选择状态

    以下是两种实现效果 1. 自定义cell 继承UITableViewCell 重写 -(void)setSelected:(BOOL)selected animated:(BOOL)animated ...

随机推荐

  1. ARM体系结构与编程-5

    GET通经常使用于包括定义常量的源文件. 比如:GET 2440addr.inc 用AREA定义一个段.ENTRY用于指定程序的入口点,END用于告诉汇编器源文件已经结束. 比如: AREA init ...

  2. Python--常用模块部分

    模块 pip install #模块名称 #安装模块 #导入模块 from collections import namedtuple collections模块 提供了几个额外的数据类型: Coun ...

  3. 图像处理之opencv---加减乘除运算cvdiv

    http://chyyeng.blog.163.com/blog/static/16918230201211632135456/

  4. C# - Garbage Collection

     The .NET Framework's garbage collector manages the allocation and release of memory for your appl ...

  5. 从架构层面杜绝lua中使用未定义的变量

    # 从架构层面杜绝lua中使用未定义的变量 标签(空格分隔): lua --- lua中有一个很坑的地方:1.就是如果一个变量拼写错误,会自动的认为你定义了一个值为nil的全局变量.2.如果在func ...

  6. 【BZOJ4668】冷战 并查集

    [BZOJ4668]冷战 Description 1946 年 3 月 5 日,英国前首相温斯顿·丘吉尔在美国富尔顿发表“铁幕演说”,正式拉开了冷战序幕. 美国和苏联同为世界上的“超级大国”,为了争夺 ...

  7. javascript 中 "undefined" 与 "is not defined" 分析

      var var1; console.log( typeof var0);//print "undefined",主要看下面对var0单独的输出 console.log( typ ...

  8. Grid++Report设置显示固定行数

    一.要实现的功能打印的报表显示固定的行数,并且设置字段的文字可以自动换行二.设置步骤1.鼠标左键单击“明细网格”栏,在右侧属性窗口中设置“追加空白行”属性值为:是:“追加空白行在后”属性值为:是.2. ...

  9. Performance Tuning Using Linux Process Management Commands

    [root@bigdata-server-02 /]# ps --help all Usage: ps [options] Basic options: -A, -e all processes -a ...

  10. Safair 浏览器cllick事件不生效或者需要双击才生效

    针对Safair 浏览器cllick事件不生效或者需要双击才生效的解决方案. 方法一:给元素加上cursor: pointer样式.(不生效) 方法二:ios事件机制不一样,将click事件改为mou ...