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

1.前言

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

2.思路

按現(xiàn)在的思路來看好像是解決不了這個收藏的問題了,我看了下微博的點贊功能,也有數(shù)據(jù)刷新但是明顯的沒有問題,所以換個思路。
更改本地數(shù)據(jù):在我點擊收藏后,請求收藏接口,接口返回成功后更改本地數(shù)據(jù),而不是再去請求刷新界面。這樣就可以實現(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  此處是暫時代替的默認圖片
    [_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;//更改本地數(shù)據(jù)
        
        if ([model.follow isEqualToString:@"true"]){
            [self requestCollectDataWithModel:model andCell:cell];
        }else {
            [self requestUncollectDataWithModel:model andCell:cell];
        }
    }
}


#pragma mark - =================是否收藏=================
//收藏(網(wǎng)絡請求)
- (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.總結(jié)

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

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

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

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

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