1.前言
項(xiàng)目需求要實(shí)現(xiàn)點(diǎn)擊收藏功能渗钉,但是頁面數(shù)據(jù)進(jìn)行了分頁功能航瞭,當(dāng)加載了第二頁數(shù)據(jù)后诫硕,收藏按鈕的顯示就紊亂,具體原因是點(diǎn)擊收藏后刊侯,請求收藏接口成功后要對數(shù)據(jù)進(jìn)行刷新章办,這個時(shí)候因?yàn)榉猪摰脑颍虞d過來的數(shù)據(jù)只是第二頁的(或者第一頁滨彻,反正只有一頁)藕届,這樣肯定是不行的。
2.思路
按現(xiàn)在的思路來看好像是解決不了這個收藏的問題了亭饵,我看了下微博的點(diǎn)贊功能休偶,也有數(shù)據(jù)刷新但是明顯的沒有問題,所以換個思路辜羊。
更改本地?cái)?shù)據(jù):在我點(diǎn)擊收藏后踏兜,請求收藏接口,接口返回成功后更改本地?cái)?shù)據(jù)只冻,而不是再去請求刷新界面庇麦。這樣就可以實(shí)現(xiàn)我們想要的功能了。
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ù)的正確性睡雇,我覺得這是個可行的辦法萌衬。如果本文對你有所幫助,請點(diǎn)贊啊它抱。