iOS UICollectionView 按钮点击变色(收藏点赞功能)实现

1.前言

项目需求要实现点击收藏功能,但是页面数据进行了分页功能,当加载了第二页数据后,收藏按钮的显示就紊乱,具体原因是点击收藏后,请求收藏接口成功后要对数据进行刷新,这个时候因为分页的原因,加载过来的数据只是第二页的(或者第一页,反正只有一页),这样肯定是不行的。本篇文章也可移步简书阅览。

2.思路

按现在的思路来看好像是解决不了这个收藏的问题了,我看了下微博的点赞功能,也有数据刷新但是明显的没有问题,所以换个思路。
更改本地数据:在我点击收藏后,请求收藏接口,接口返回成功后更改本地数据,而不是再去请求刷新界面。这样就可以实现我们想要的功能了。

未命名gif.gif

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数据,刷新界面,本地处理不仅免除了再次加载的耗时,还能保证更新的数据的正确性,我觉得这是个可行的办法。如果本文对你有所帮助,请点赞啊。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值