iOS UICollectionView 按钮点击变色(收藏点赞功能)实现
2017-05-24 13:22
369 查看
1.前言
项目需求要实现点击收藏功能,但是页面数据进行了分页功能,当加载了第二页数据后,收藏按钮的显示就紊乱,具体原因是点击收藏后,请求收藏接口成功后要对数据进行刷新,这个时候因为分页的原因,加载过来的数据只是第二页的(或者第一页,反正只有一页),这样肯定是不行的。本篇文章也可移步简书阅览。2.思路
按现在的思路来看好像是解决不了这个收藏的问题了,我看了下微博的点赞功能,也有数据刷新但是明显的没有问题,所以换个思路。更改本地数据:在我点击收藏后,请求收藏接口,接口返回成功后更改本地数据,而不是再去请求刷新界面。这样就可以实现我们想要的功能了。
3.主要代码
这里使用的是UICollectionView自定义cell
#import <UIKit/UIKit.h> #import "BrandSearchModel.h" //@class 引入自己 @class BrandSearchResultCollectCell; //代理方法中要将cell带过去 @protocol BrandSearchResultCollectCellDelegate <NSObject> - (void)onFavourButtonClick:(BrandSearchResultCollectCell *)cell; @end @interface BrandSearchResultCollectCell : UICollectionViewCell //传索引过来 @property (nonatomic) NSIndexPath *indexPath; @property (nonatomic, strong) BrandSearchModel *model; @property (nonatomic, weak) id<BrandSearchResultCollectCellDelegate> delegate; @end
#import "BrandSearchResultCollectCell.h" @implementation BrandSearchResultCollectCell //... -(void)setModel:(BrandSearchModel *)model{ _model = model; //brand_s_default_110x75 此处是暂时代替的默认图片 [_logoImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.image]] placeholderImage:[UIImage imageNamed:@"brand_s_default_110x75"]]; _titleLabel.text = model.tmname; _statusLabel.text = model.tmlaw; _favNumLabel.text = model.intcls; if ([model.follow isEqualToString:@"false"]) { _favButton.selected = NO;//没收藏 }else{ _favButton.selected = YES;//收藏 } } //按钮点击 - (void)favButtonAction{ if (_delegate && [_delegate respondsToSelector:@selector(onFavourButtonClick:)]){ [_delegate onFavourButtonClick:self]; } }
ViewController
//... //cell的记载 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { BrandSearchResultCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath]; cell.model = self.dataArray[indexPath.row]; //要把索引传过去后面用的到 cell.indexPath = indexPath; cell.delegate = self; return cell; } //... #pragma mark - BrandSearchResultCollectCellDelegate cell代理 - (void)onFavourButtonClick:(BrandSearchResultCollectCell *)cell{ if (cell.indexPath.row < [self.dataArray count]) { BrandSearchModel *model = self.dataArray[cell.indexPath.row]; //follow为true为收藏,false为未收藏 NSString *follow = [model.follow isEqualToString:@"false"] ? @"true" : @"false"; model.follow = follow;//更改本地数据 if ([model.follow isEqualToString:@"true"]){ [self requestCollectDataWithModel:model andCell:cell]; }else { [self requestUncollectDataWithModel:model andCell:cell]; } } } #pragma mark - =================是否收藏================= //收藏(网络请求) - (void)requestCollectDataWithModel:(BrandSearchModel *)model andCell:(BrandSearchResultCollectCell *)cell{ NSMutableDictionary *params = [CBConnect getBaseRequestParams]; [params setValue:model.cxkey forKey:@"cxkey"];//商标注册号 [params setValue:model.intcls forKey:@"intcls"];//商标国际分类 [CBConnect getBrandCollectTradeMark:params success:^(id responseObject) { //请求成功则刷新 [self.mCollectionView reloadItemsAtIndexPaths:@[cell.indexPath]]; } successBackfailError:^(id responseObject) { model.follow = @"false";//失败的话则恢复原来的值 } failure:^(NSURLSessionDataTask *operation, NSError *error) { }]; } //取消收藏 - (void)requestUncollectDataWithModel:(BrandSearchModel *)model andCell:(BrandSearchResultCollectCell *)cell{ NSMutableDictionary *params = [CBConnect getBaseRequestParams]; [params setValue:model.cxkey forKey:@"cxkey"];//商标注册号 [params setValue:model.intcls forKey:@"intcls"];//商标国际分类 [CBConnect getBrandUncollectTradeMark:params success:^(id responseObject) { //请求成功则刷新 [self.mCollectionView reloadItemsAtIndexPaths:@[cell.indexPath]]; } successBackfailError:^(id responseObject) { model.follow = @"true";//失败的话则恢复原来的值 } failure:^(NSURLSessionDataTask *operation, NSError *error) { }]; }
4.总结
其实最主要的一点是本地来处理收藏与取消收藏后数据,而不是收藏后再去请求一下List数据,刷新界面,本地处理不仅免除了再次加载的耗时,还能保证更新的数据的正确性,我觉得这是个可行的办法。如果本文对你有所帮助,请点赞啊。相关文章推荐
- iOS UICollectionViewCell上有Button点击实现收藏效果
- iOS实现按钮点击选中与被选中切换功能
- iOS 利用UICollectionView拖拽排序 实现的仿照腾讯新闻频道管理功能 XLChannelControl
- iOS Swift利用UICollectionView实现无限轮播功能(原理)详解
- JS实现PC、Android、IOS端的点击按钮复制内容功能
- 微信小程序实现点击按钮修改view标签背景颜色功能示例【附demo源码下载】
- [iOS]UICollectionView循环滚动功能的实现思路
- 微信小程序实现点击按钮移动view标签的位置功能示例【附demo源码下载】
- iOS WebView 如何通过js获取网页中所有图片并加入点击事件,实现浏览图片的功能
- iOS引导登录、登录引导、login(点击某个功能按钮需要登录并且登录完成后自动跳转到要去的功能实现方法)
- iOS 点击按钮实现拨打电话,发送短信功能
- iOS高级开发——CollectionView的cell中按钮的点击实现
- Android实现点击按钮变色功能
- iOS高级开发——CollectionView的cell中按钮的点击实现
- iOS-点击按钮实现复制功能
- 进击的KFC:iOS WebView 如何通过js获取网页中所有图片并加入点击事件,实现浏览图片的功能
- QML实现-按钮点击变色功能
- iOS实现点击按钮,跳转到与mian.storyboard 中与ViewController 关联的页面
- 类似于coc点击好友列表右侧显示的visit等功能按钮的view的处理方法
- 【IOS】扩展UIImageViewEx实现:手势移动,旋转,缩放(附带一个收缩的文字标签功能)