你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS購物分類模塊的實現方案

iOS購物分類模塊的實現方案

編輯:IOS開發綜合

本文實例分享了iOS購物分類模塊的實現方案,供大家參考,具體內容如下

啟動
在AppDelegate中創建主視圖控制器。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.
  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  // Override point for customization after application launch.
  self.window.backgroundColor = [UIColor whiteColor];
  [self.window makeKeyAndVisible];
      
  Basetabarcontroller*base=[[Basetabarcontroller alloc]init];
      
  self.window.rootViewController=base;
      
  return YES;
    
}

UI框架設計

Basetabbarcontroller基於UITabBarController,作為主視圖控制器。
頭文件如下:

@interface Basetabarcontroller : UITabBarController
    
@end

實現文件中主要做了2點:創建視圖控制器ClassViewController,並將它設置到新創建的創建導航控制器中。

- (void)viewDidLoad {
  [super viewDidLoad];
  self.tabBar.tintColor = [UIColor redColor];
  // self.tabBar.barTintColor = [UIColor blackColor];
      
   ClassViewController*classList=[[ClassViewController alloc]init];
  //classList.title=@"分類";
     
     
  [self addChildViewController:classList title:@"分類" image:@""];
     
  UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  menuBtn.frame = CGRectMake(0, 0, 20, 18);
  [menuBtn setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
  [menuBtn addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
  self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
  //self.tabBar.barTintColor = [UIColor redColor];
}
- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title image:(NSString *)image{
      
  UINavigationController *childVC = [[UINavigationController alloc]initWithRootViewController:childController];
  childVC.tabBarItem.title = title;
   childVC.tabBarItem.image = [UIImage imageNamed:image];
  childVC.navigationBar.barTintColor = [UIColor whiteColor];
       
  [self addChildViewController:childVC];
      
    
}

主要的界面視圖控制器定義:

@interface ClassViewController : UIViewController
   
@end

實現文件中:

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
     
   
  UIBarButtonItem *searchBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchBarButtonItemAction)];
  self.navigationItem.rightBarButtonItem = searchBarButtonItem;
    [self setdata];
  [self setimage];
  [self setdeatil];
   
     
  _a=1;
  _b=1;
  _segement=[[UISegmentedControl alloc]initWithItems:@[@"攻略",@"詳情"]];
  _segement.frame=CGRectMake(90, 20, kwidth-180, 30);
  _segement.tintColor=[UIColor blackColor];
  _segement.selectedSegmentIndex=0;
  [_segement addTarget:self action:@selector(changevalue:) forControlEvents:(UIControlEventValueChanged)];
  self.navigationItem.titleView =_segement;
   self.scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kwidth, KHeight)];
  _scrollview.directionalLockEnabled=YES;
  _scrollview.contentSize=CGSizeMake(kwidth*2, KHeight);
  _scrollview.delegate=self;
     
     
  [self.view addSubview:self.scrollview];
  UICollectionViewFlowLayout*flow=[[UICollectionViewFlowLayout alloc]init];
    //列距
  flow.minimumInteritemSpacing=20;
     
  //行距
  flow.minimumLineSpacing=40;
    //分區內邊距
  flow.sectionInset=UIEdgeInsetsMake(0, 20, 20, 20);
    CGFloat totalwidth=self.view.frame.size.width;
     
  CGFloat itemwidth=(totalwidth-2*20-3*20)/4.0;
     
  CGFloat itemheight=itemwidth;
     
  flow.itemSize=CGSizeMake(itemwidth, itemheight);
     
  flow.headerReferenceSize=CGSizeMake(0, 40);
     
  //滾動方向
  flow.scrollDirection= UICollectionViewScrollDirectionVertical;
  ;
     
  //區頭大小
  flow.headerReferenceSize=CGSizeMake(0, 100);
     
  _collection=[[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flow];
  _collection.backgroundColor=[UIColor whiteColor];
  _collection.tag=1;
  _Srr=@[@"1",@"s",@"2",@"r"];
  for (NSString*St in _Srr) {
    [_collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:St];}
  //設置 數據源 和代理
  _collection.dataSource=self;
  _collection.delegate=self;
  [_collection registerClass:[ClassCollectionViewCell class] forCellWithReuseIdentifier:@"mycell"];
  // _collection.backgroundColor=[UIColor yellowColor];
  _collection.directionalLockEnabled=YES;
     
  [self.scrollview addSubview:_collection];
  UIView*view=[[UIView alloc]initWithFrame:CGRectMake(kwidth, 0, kwidth, 30)];
  // view.backgroundColor = [UIColor whiteColor];
  UIButton*label=[UIButton buttonWithType:(UIButtonTypeSystem)];
  label.frame=CGRectMake(0, 20, 200, 14) ;
  [label setTitle:@"選禮神器" forState:(UIControlStateNormal)];
  [label addTarget:self action:@selector(xuan) forControlEvents:(UIControlEventTouchUpInside)];
  [view addSubview:label];
  [self.scrollview addSubview:view];

 網絡數據封裝
基於NFNetworking,封裝了3個接口

@interface LORequestManger : NSObject
   
+ (void)POST:(NSString *)URL params:(NSDictionary * )params success:(void (^)(id response))success
   failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error;
   
   
+ (void)GET:(NSString *)URL
  success:(void (^)(id response))success
  failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error;
   
+ (void)UPLOADIMAGE:(NSString *)URL
       params:(NSDictionary *)params
    uploadImage:(UIImage *)image
      success:(void (^)(id response))success
      failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error;
   
@end

實現文件:

#import "LORequestManger.h"
#define serverUrl @"http://192.168.1.1:8080/jiekou"
   
@implementation LORequestManger
   
   
   
+ (void)POST:(NSString *)URL params:(NSDictionary * )params success:(void (^)(id response))success
   failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error
{
  // 創建請求管理者
  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
     
  // 請求超時時間
   
  manager.requestSerializer.timeoutInterval = 30;
  NSString *postStr = URL;
  if (![URL hasPrefix:@"http"]) {
       
    postStr = [NSString stringWithFormat:@"%@%@", serverUrl,URL] ;
  }
  NSMutableDictionary *dict = [params mutableCopy];
     
     
  // 發送post請求
  [manager POST:postStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
       
    // 請求成功
    NSDictionary *responseDict = (NSDictionary *)responseObject;
    success(responseDict);
       
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {// 請求失敗
    Error( operation,error);
       
  }];
   
   
}
   
+ (void)GET:(NSString *)URL
  success:(void (^)(id response))success
  failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error
{
  // 獲得請求管理者
  AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
  manager.requestSerializer = [AFJSONRequestSerializer serializer];
  [manager.requestSerializer setHTTPShouldHandleCookies:NO];
  manager.requestSerializer.timeoutInterval = 30;
  NSString *getStr = URL;
//  NSLog(@"getStr======%@",getStr);
  if (![URL hasPrefix:@"http"]) {
       
    getStr = [NSString stringWithFormat:@"%@%@", serverUrl,URL] ;
  }
   
     
  // 發送GET請求
  [manager GET:getStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//    NSLog(@"getStr------------%@",getStr);
    NSDictionary *responseDict = (NSDictionary *)responseObject;
      
      success(responseDict);
   
       
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if (!operation.responseObject) {
      NSLog(@"網絡錯誤");
    }
    Error( operation,error);
  }];
   
     
}
   
+ (void)UPLOADIMAGE:(NSString *)URL
       params:(NSDictionary *)params
    uploadImage:(UIImage *)image
      success:(void (^)(id response))success
      failure:(void (^)(AFHTTPRequestOperation *operation,NSError *error))Error
{
  // 創建請求管理者
  AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
     
  manager.requestSerializer = [AFJSONRequestSerializer serializer];
  manager.responseSerializer = [AFJSONResponseSerializer serializer];
  manager.requestSerializer.timeoutInterval = 30;
  //  [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  //
  //  [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
     
  NSString *postStr = [NSString stringWithFormat:@"%@%@", serverUrl,URL] ;
  NSMutableDictionary *dict = [params mutableCopy];
   
  [manager POST:postStr parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
    [formData appendPartWithFileData:imageData name:@"img" fileName:@"head.jpg" mimeType:@"image/jpeg"];
       
  } success:^(AFHTTPRequestOperation *operation, id responseObject) {
   
    NSDictionary *responseDict = (NSDictionary *)responseObject;
        success(responseDict);
    
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    Error( operation,error);
       
  }];
   
   
   
}

效果:

以上就是本文的全部內容,希望對大家的學習有所幫助。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved