Notification与Delegate实现通讯沙拉实例
来源:互联网 发布:诸宸的悲剧 知乎 编辑:程序博客网 时间:2024/06/10 12:06
这个例子是我在AppStore上下的一个沙拉的app模仿做的,现在还没做文件管理。
黄星星就是收藏的意思,在navigationBar上面有个按钮查看收藏夹内容的,黄星星是做了个button,开始想做view,但是没法定位view的位置,我在UITableViewCell没能实现,主要尝试是hittest,layer和view的方式都试过了。然后再说这个button,在改变收藏状态后是用delegate实现的通讯
UITableViewCell的子类中设定一个delegate,在收藏和取消收藏时分别调用delegate的一个方法
<pre name="code" class="objc">@protocol ListTableViewCellDelegate <NSObject>@optional-(void)insertNewCell:(UITableViewCell *)cell;-(void)deleteNewCell:(UITableViewCell *)cell;@end
@property (nonatomic)id<ListTableViewCellDelegate>delegate;
-(void)collect:(UIButton *)btn{ self.collected = !self.collected;}
-(void)setCollected:(BOOL)collected{ _collected = collected; if(collected){ [self.collectionButton setImage:[UIImage imageNamed:@"yellowStar"] forState:UIControlStateNormal]; if(self.delegate && [self.delegate conformsToProtocol:@protocol(ListTableViewCellDelegate) ]){ [self.delegate insertNewCell:self]; }else{ } }else{ [self.collectionButton setImage:[UIImage imageNamed:@"Star"] forState:UIControlStateNormal]; if(self.delegate && [self.delegate conformsToProtocol:@protocol(ListTableViewCellDelegate) ]){ [self.delegate deleteNewCell:self]; } } [self setNeedsLayout];}
然后在viewController中实现这个delegate,将收藏的cell加入一个array中,取消收藏的移除出该array;
-(void)insertNewCell:(UITableViewCell *)cell{ NSIndexPath *index = [self.ListTableView indexPathForCell:cell]; [colletedArray addObject:saladList[index.row]]; }-(void)deleteNewCell:(UITableViewCell *)cell{ NSIndexPath *index = [self.ListTableView indexPathForCell:cell]; [colletedArray removeObject:saladList[index.row]]; }
这里要记住在cell中实现改delegate,就是cell.delegate = self;
没有做文件管理,所以都是暂时的。。。
然后再是Notification的内容,收藏夹的页面和上一个页面是一样的,收藏夹内也可以取消收藏,这时就需要和主页面同步。
收藏夹的UITableViewController中的cell也实现了delegate
这里cell上加了个tag进行识别
下面是收藏夹的TVC内容
-(void)insertNewCell:(UITableViewCell *)cell{ NSDictionary *userInfo = @{@"tag" : @(cell.tag)}; [[NSNotificationCenter defaultCenter]postNotificationName:ADDCOLLECTNOTIFICATION object:self userInfo:userInfo]; }-(void)deleteNewCell:(UITableViewCell *)cell{ NSLog(@"delete01"); NSDictionary *userInfo = @{@"tag" : @(cell.tag)}; [[NSNotificationCenter defaultCenter]postNotificationName:DELETECOLLECTNOTIFICATION object:self userInfo:userInfo]; }
再在主页面上
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserverForName:ADDCOLLECTNOTIFICATION object:nil queue:nil usingBlock:^(NSNotification *note) { int tagNum = [note.userInfo[@"tag"] intValue]; for(ListTableViewCell *cell in [self.ListTableView visibleCells]){ if(cell.tag == tagNum){ cell.collected = YES; } } }]; [[NSNotificationCenter defaultCenter] addObserverForName:DELETECOLLECTNOTIFICATION object:nil queue:nil usingBlock:^(NSNotification *note) { int tagNum = [note.userInfo[@"tag"] intValue]; for(ListTableViewCell *cell in [self.ListTableView visibleCells]){ if(cell.tag == tagNum){ NSLog(@"delete"); cell.collected = NO; } } }]; }
0 0
- Notification与Delegate实现通讯沙拉实例
- Delegate 与 Notification
- delegate 与 notification 的区别
- android Notification的实例与Notification API
- Java Websocket实例【服务端与客户端实现全双工通讯】
- iOS通讯模式(KVO、Notification、Delegate、Block、Target-Action的区别)
- iOS通讯模式(KVO、Notification、Delegate、Block、Target-Action的区别)
- delegate, notification和kvo模式实现通信的优缺点
- delegate,notification,KVO三种模式实现通信的优缺点
- iOS开发:Delegate、Notification和KVO的联系与区别
- 代理delegate与通知Notification、block的使用区别
- Delegate,Notification,KVO
- Delegate,Notification,KVO
- Delegate,Notification,KVO
- delegate、notification和KVO
- delegate,notification,kvo比较
- Delegate, Notification,KVO优缺点
- NOTification Delegate 区别
- 实验一 离散时间序列卷积和MATLAB实现
- Labview与其他应用程序的接口设计
- OJ-多态性
- 深度学习及课题研究杂谈(一)
- Python偏函数
- Notification与Delegate实现通讯沙拉实例
- 算法——排序算法个人总结
- Android <include />用法
- struts2中json插件日期格式化
- qq连接代码生成总结
- Archlinux 修改/etc/profile文件使得ls等命令结果显示颜色
- 编程之美 2.9 斐波那契(Fibonacci)数列
- 通径分析
- UML简单介绍(二十一)——RUP软件工程模型介绍(下)