本文實例分享了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];
收集數據封裝
基於N.networking,封裝了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);
}];
}
後果:


以上就是本文的全體內容,願望對年夜家的進修有所贊助。
【iOS購物分類模塊的完成計劃】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!