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