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ù)的正確性,我覺得這是個可行的辦法。如果本文對你有所幫助,請點贊啊。