當前位置:吉日网官网 - 紀念幣收藏 - 如何在iosapp中編寫收藏代碼

如何在iosapp中編寫收藏代碼

NSString * const MTDone = @ " done

NSString * const MTEdit = @ " edit

# define MTString(str)[ns string string with format:@“% @”,str]

@ interface MTCollectViewController()& lt;MTDealCellDelegate & gt

@property(非原子,弱)UIImageView * noDataView

@property(非原子,強)NSMutableArray * deals

@property (nonatomic,assign)int current page;

@property (nonatomic,strong)UIBarButtonItem * back item;

@property (nonatomic,strong)UIBarButtonItem * selectAllItem;

@property (nonatomic,strong)UIBarButtonItem * unselect allitem;

@property (nonatomic,strong)UIBarButtonItem * remove item;

@end

@ implementation MTCollectViewController

- (UIBarButtonItem *)backItem

{

如果(!_backItem) {

self . back item =[UIBarButtonItem item with target:self action:@ selector(back)image:@ " icon _ back " high image:@ " icon _ back _ highlight "];

}

return _ backItem

}

- (UIBarButtonItem *)選擇所有項目

{

如果(!_selectAllItem) {

自我。select all item =[[uibarbuttonitemalloc]initwithttitle:mt string(@ " select all ")style:uibarbuttonitemstyledone target:self action:@ selector(select all)];

}

return _ selectAllItem

}

- (UIBarButtonItem *)取消選擇項目

{

如果(!_unselectAllItem) {

自我。unselect allitem =[[uibarbuttonitemalloc]initwithttitle:mt string(@ " Select None ")style:uibarbuttonitemstyledone target:self action:@ selector(unselect all)];

}

return _ unselectAllItem

}

- (UIBarButtonItem *)removeItem

{

如果(!_removeItem) {

self . remove item =[[uibarbuttonitemalloc]initwithttitle:mt string(@ " delete ")style:uibarbuttonitemstyledone target:self action:@ selector(remove)];

self . remove item . enabled = NO;

}

return _ removeItem

}

- (NSMutableArray *)交易

{

如果(!_deals) {

self . deals =[[NSMutableArray alloc]init];

}

退貨_交易;

}

- (UIImageView *)無數據視圖

{

如果(!_noDataView) {

//添加“無數據”的提醒

ui imageview * no data view =[[ui imageview alloc]init with image:[ui image image named:@ " icon _ collects _ empty "]];

【self . view add subview:no data view】;

[no data view autocentinserview];

self . no data view = no data view;

}

return _ noDataView

}

static ns string * const reuse identifier = @ " deal ";

-(實例類型)初始化

{

UICollectionViewFlowLayout * layout =[[UICollectionViewFlowLayout alloc]init];

//單元格的大小

layout.itemSize = CGSizeMake(305,305);

return[self initWithCollectionViewLayout:layout];

}

- (void)viewDidLoad {

【超級viewDidLoad】;

Self.title = @ "團購收藏";

self . collection view . background color = MTGlobalBg;

//在左側返回

self . navigation item . leftbarbuttonitems = @[self . back item];

//註冊單元格類

[self . collection view register nib:[UINib nibWithNibName:@ " mtdealcoell " bundle:nil]forCellWithReuseIdentifier:reuseIdentifier];

self . collection view . alwaysbouncevertical = YES;

//加載第壹頁的收藏夾數據。

[self loadMoreDeals];

//偵聽收集狀態更改的通知。

[MTNotificationCenter add observer:self selector:@ selector(collectStateChange:)name:MTCollectStateDidChangeNotification object:nil];

//添加並加載。

[self . collection view addFooterWithTarget:self action:@ selector(loadmoreandles)];

//設置導航欄內容

self . navigation item . rightbarbuttonitem =[[UIBarButtonItem alloc]init with title:MTEdit style:UIBarButtonItemStyleDone目標:self action:@ selector(edit:);

}

- (void)編輯:(UIBarButtonItem *)項目

{

if([item . title isEqualToString:MTEdit]){

item.title = MTDone

self . navigation item . leftbarbuttonitems = @[self . back item,self.selectAllItem,self.unselectAllItem,self . remove item];

//進入編輯狀態。

for (MTDeal *deal in self.deals) {

deal.editing =是;

}

}否則{

item.title = MTEdit

self . navigation item . leftbarbuttonitems = @[self . back item];

//結束編輯狀態

for (MTDeal *deal in self.deals) {

deal.editing =否;

}

}

//刷新表格

[self . collection view reload data];

}

- (void)選擇全部

{

for (MTDeal *deal in self.deals) {

deal.checking =是;

}

[self . collection view reload data];

self . remove item . enabled = YES;

}

- (void)取消選擇

{

for (MTDeal *deal in self.deals) {

deal.checking =否;

}

[self . collection view reload data];

self . remove item . enabled = NO;

}

-(無效)刪除

{

NSMutableArray * tempArray =[NSMutableArray數組];

for (MTDeal *deal in self.deals) {

if (deal.isChecking) {

[MTDealTool removeCollectDeal:deal];

[tempArray add object:deal];

}

}

//刪除所有選中的模型。

[self . deals removeObjectsInArray:tempArray];

[self . collection view reload data];

self . remove item . enabled = NO;

}

- (void)加載更多交易

{

// 1.增加頁碼

self . current page++;

// 2.添加新數據

[self . deals addObjectsFromArray:[MTDealTool collect deals:self . current page]];

// 3.刷新表格

[self . collection view reload data];

// 4.結束刷新

[self . collection view footerEndRefreshing];

}

-(void)collectStateChange:(ns notification *)通知

{

//if([notification . userinfo[MTIsCollectKey]boolValue]){

////收集成功

//[self . deals insertObject:notification . userinfo[MTCollectDealKey]at index:0];

// }其他{

////已成功取消收集

//[self . deals remove object:notification . userinfo[MTCollectDealKey]];

// }

//

//[self . collection view reload data];

[self . deals remove all objects];

self . current page = 0;

[self loadMoreDeals];

}

-(無效)返回{

[self dismissViewControllerAnimated:是完成:無];

}

#雜註標記-單元格的代理

-(void)dealCellCheckingStateDidChange:(mtdealcoell *)單元格

{

BOOL hasChecking = NO

for (MTDeal *deal in self.deals) {

if (deal.isChecking) {

hasChecking = YES

打破;

}

}

//根據是否有復選框來決定刪除按鈕是否可用。

self . remove item . enabled = has checking;

}

/**

當屏幕旋轉時,控制器視圖的大小會改變。

*/

-(void)viewlltransitiontosize:(CGSize)size with transition coordinator:(id & lt;UIViewControllerTransitionCoordinator & gt;)協調員

{

//根據屏幕寬度確定列數。

int cols =(size . width = = 1024)?3 : 2;

//根據列數計算內邊距。

UICollectionViewFlowLayout * layout =(UICollectionViewFlowLayout *)self . collectionview layout;

CG float inset =(size . width-cols * layout . itemsize . width)/(cols+1);

layout . section Inset = uiedgeinsets make(inset,inset,inset,inset);

//設置每行之間的間距。

layout.minimumLineSpacing = 50

}

# pragma mark & ltUICollectionViewDataSource & gt

-(NSInteger)collection view:(UICollectionView *)collection view number of itemsinsection:(NSInteger)section {

//計算壹次內邊距。

[self view willtransitiontosize:CGSizeMake(collection view . width,0)with transition coordinator:nil];

//控制尾部控件的顯示和隱藏。

self . collection view . footer hidden =(self . deals . count = =[MTDealTool collectDealsCount]);

//控制“無數據”的提醒

self . no data view . hidden =(self . deals . count!= 0);

返回self . deals . count;

}

-(UICollectionView cell *)collection view:(UICollectionView *)collection view cellForItemAtIndexPath:(NSIndexPath *)index path {

MTDealCell * cell =[collection view dequeueReusableCellWithReuseIdentifier:reuseIdentifier for index path:index path];

cell.delegate = self

cell . deal = self . deals[index path . item];

返回單元格;

}

# pragma mark & ltUICollectionViewDelegate & gt

-(void)collection view:(UICollectionView *)collection view didSelectItemAtIndexPath:(NSIndexPath *)index path

{

MTDetailViewController * detail VC =[[MTDetailViewController alloc]init];

detail VC . deal = self . deals[index path . item];

[self present view controller:detail VC animated:YES completion:nil];

}

  • 上一篇:如何買床
  • 下一篇:宋朝清明上河圖中有多少人?人數是怎麽算出來的?
  • copyright 2024吉日网官网