iOS UICollectionView 上按鈕點(diǎn)擊變色(收藏功能)

1.前言

項(xiàng)目需求要實(shí)現(xiàn)點(diǎn)擊收藏功能,但是頁面數(shù)據(jù)進(jìn)行了分頁功能,當(dāng)加載了第二頁數(shù)據(jù)后,收藏按鈕的顯示就紊亂,具體原因是點(diǎn)擊收藏后,請求收藏接口成功后要對數(shù)據(jù)進(jìn)行刷新,這個(gè)時(shí)候因?yàn)榉猪摰脑?,加載過來的數(shù)據(jù)只是第二頁的(或者第一頁,反正只有一頁),這樣肯定是不行的。

2.思路

按現(xiàn)在的思路來看好像是解決不了這個(gè)收藏的問題了,我看了下微博的點(diǎn)贊功能,也有數(shù)據(jù)刷新但是明顯的沒有問題,所以換個(gè)思路。
更改本地?cái)?shù)據(jù):在我點(diǎn)擊收藏后,請求收藏接口,接口返回成功后更改本地?cái)?shù)據(jù),而不是再去請求刷新界面。這樣就可以實(shí)現(xiàn)我們想要的功能了。

未命名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  此處是暫時(shí)代替的默認(rèn)圖片
    [_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;//收藏
    }
}


//按鈕點(diǎn)擊
- (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;//更改本地?cái)?shù)據(jù)
        
        if ([model.follow isEqualToString:@"true"]){
            [self requestCollectDataWithModel:model andCell:cell];
        }else {
            [self requestUncollectDataWithModel:model andCell:cell];
        }
    }
}


#pragma mark - =================是否收藏=================
//收藏(網(wǎng)絡(luò)請求)
- (void)requestCollectDataWithModel:(BrandSearchModel *)model andCell:(BrandSearchResultCollectCell *)cell{

    NSMutableDictionary *params = [CBConnect getBaseRequestParams];
    [params setValue:model.cxkey forKey:@"cxkey"];//商標(biāo)注冊號
    [params setValue:model.intcls forKey:@"intcls"];//商標(biāo)國際分類

    [CBConnect getBrandCollectTradeMark:params success:^(id responseObject) {
        //請求成功則刷新
         [self.mCollectionView reloadItemsAtIndexPaths:@[cell.indexPath]];
    } successBackfailError:^(id responseObject) {
         model.follow = @"false";//失敗的話則恢復(fù)原來的值
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        
    }];
}

//取消收藏
- (void)requestUncollectDataWithModel:(BrandSearchModel *)model andCell:(BrandSearchResultCollectCell *)cell{
    
    NSMutableDictionary *params = [CBConnect getBaseRequestParams];
    [params setValue:model.cxkey forKey:@"cxkey"];//商標(biāo)注冊號
    [params setValue:model.intcls forKey:@"intcls"];//商標(biāo)國際分類
    
    [CBConnect getBrandUncollectTradeMark:params success:^(id responseObject) {
     //請求成功則刷新
         [self.mCollectionView reloadItemsAtIndexPaths:@[cell.indexPath]];

    } successBackfailError:^(id responseObject) {
        model.follow = @"true";//失敗的話則恢復(fù)原來的值
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        
    }];
}

4.總結(jié)

其實(shí)最主要的一點(diǎn)是本地來處理收藏與取消收藏后數(shù)據(jù),而不是收藏后再去請求一下List數(shù)據(jù),刷新界面,本地處理不僅免除了再次加載的耗時(shí),還能保證更新的數(shù)據(jù)的正確性,我覺得這是個(gè)可行的辦法。如果本文對你有所幫助,請點(diǎn)贊啊。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,355評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,704評論 4 61
  • Android Context 是什么? - 廢墟的樹的專欄 - 博客頻道 - CSDN.NET
    浩南閱讀 229評論 0 2
  • 插曲一 剛剛在簡書寫了兩百左右文字,沒保存,不小心給刪掉了。這個(gè)意外的小插曲導(dǎo)致大腦一片空白,想不起如果重新開始的...
    綠蘿寶貝閱讀 396評論 8 4
  • 曾經(jīng)有一樣?xùn)|西擺在我面前,我不知道珍惜,當(dāng)失去時(shí)才追悔莫及,這就是健康。。關(guān)注女性健康,學(xué)會好好愛自己。愛自己,才...
    一字一詞閱讀 269評論 0 1

友情鏈接更多精彩內(nèi)容