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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,034评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,488评论 4 61
  • Android Context 是什么? - 废墟的树的专栏 - 博客频道 - CSDN.NET
    浩南阅读 1,531评论 0 2
  • 插曲一 刚刚在简书写了两百左右文字,没保存,不小心给删掉了。这个意外的小插曲导致大脑一片空白,想不起如果重新开始的...
    绿萝宝贝阅读 2,675评论 8 4
  • 曾经有一样东西摆在我面前,我不知道珍惜,当失去时才追悔莫及,这就是健康。。关注女性健康,学会好好爱自己。爱自己,才...
    一字一词阅读 1,724评论 0 1

友情链接更多精彩内容