您的位置:首页 > 产品设计 > UI/UE

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数据,刷新界面,本地处理不仅免除了再次加载的耗时,还能保证更新的数据的正确性,我觉得这是个可行的办法。如果本文对你有所帮助,请点赞啊。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐