iOS Group Purchase category page (9 Gongge)

Source: Internet
Author: User
Tags uikit

=-= naming some errors, but feature implementation, later notice the naming specification

WJViewGroup.h

#import<UIKit/UIKit.h>@interfaceWjviewgroup:uiview {Nsinteger _width; Nsinteger _height;} @property (nonatomic,assign) Nsinteger widthnum; //how many of the horizontal@property (nonatomic,assign) Nsinteger heightnum;//How many vertical rows?@property (copy, nonatomic)void(^itemblock) (Nsinteger index);//button click Callback@property (nonatomic,assign) BOOL isline; @property (nonatomic,strong) Nsmutablearray*Array;/** * Add Item * * @param widthitem High quantity * @param heightitem wide quantity * @param num Item Quantity*/- (void) AddItem: (Nsinteger) Widthitem Widthitem: (nsinteger) Heightitem num: (Nsarray *) num;/** * Change the text of item * * @param an array of array literals*/- (void) Item: (Nsarray *) array;@end

wjviewgroup.m

#import "WJViewGroup.h"@implementationWjviewgroup-(Instancetype) initWithFrame: (CGRect) frame { self=[Super Initwithframe:frame]; if(self) {_width=Frame.size.width; Self.backgroundcolor=[Uicolor Whitecolor]; _height=Frame.size.height; Self.layer.borderWidth=1; Self.clipstobounds=YES; Self.layer.cornerRadius=5; _widthnum=3; _heightnum=3; _array=[[Nsmutablearray alloc]init]; }    returnSelf ;}#pragmaMark-Add Item 3 line----------(void) AddItem: (Nsinteger) Widthitem Widthitem: (nsinteger) Heightitem num: (Nsarray *) num{Nsinteger index=0; Nsinteger Indenume=0;  for(Nsinteger i =0; i < Widthitem; i + +) {Nsinteger index_y=0;  for(Nsinteger j =0; J < Heightitem; J + +) {                     if(Indenume <num.count) {UIButton*button = [[UIButton alloc]initwithframe:cgrectmake (_width/_widthnum*index_y, _height/_heightnum * index, _width/_ Widthnum, _height/_heightnum)];                [Button Settitle:num[indenume] forstate:uicontrolstatenormal]; Button.tag=Indenume; Button.titleLabel.font= [Uifont systemfontofsize: -];                [Button Settitlecolor:[uicolor Graycolor] forstate:uicontrolstatenormal];                [Button addtarget:self action: @selector (buttonpressed:) forcontrolevents:uicontroleventtouchupinside];                [Self Addsubview:button];                [_array Addobject:button]; if(_isline && Index_y <2) {UIView*linevertical = [[UIView alloc]initwithframe:cgrectmake (Cgrectgetmaxx (Button.frame), Cgrectgetminy (Button.frame) + _ Height/_heightnum *0.2,0.5, _height/_heightnum *0.6)]; Linevertical.backgroundcolor=[Uicolor Lightgraycolor];                [Self addsubview:linevertical]; }} Indenume++; Index_y++; } Index++; }    if(_isline) {[Self addcrossline:0]; } }- (void) Item: (Nsarray *) Array {Nsinteger index=0;  for(UIButton *buttoninch_array) {        if(Index <array.count) {[button settitle:array[index] forstate:uicontrolstatenormal]; } Index++; }}#pragmaMark-Add a line-(void) Addcrossline: (Nsinteger) Index {if(Index = =0) {         for(inti =0; I <3; i + +) {            if(I >0) {UIView*crossline = [[UIView alloc]initwithframe:cgrectmake (_width *0.05, _height/_heightnum*i, _width *0.9,0.5)]; Crossline.backgroundcolor=[Uicolor Lightgraycolor];            [Self addsubview:crossline]; }         }    }Else {         for(inti =0; I <3; i + +) {            if(I >0) {UIView*crossline = [[UIView alloc]initwithframe:cgrectmake (0, _height/_heightnum*i, _width,0.5)]; Crossline.backgroundcolor=[Uicolor Lightgraycolor];            [Self addsubview:crossline]; }        }    }}- (void) Addverticalline { for(inti =0; I <3; i + +) {        if(I <2) {UIView*verticalline = [[UIView alloc]initwithframe:cgrectmake (_width/_widthnum * (i+1),0, _width,0.5)]; Verticalline.backgroundcolor=[Uicolor Graycolor];        [Self addsubview:verticalline]; }    }}#pragmaMark-Event Monitoring-(void) buttonpressed: (UIButton *) Sender {if(Sender.titleLabel.text.length >0) {        if(_itemblock) {_itemblock (Sender.tag); }    }}@end

Use:

1. Direct use

Wjviewgroup *group = [[Wjviewgroup alloc]initwithframe:cgrectmake (screenwidth*0.05, -, screenwidth*0.9, -)]; Group.isline=YES; Group.layer.borderColor= [Uicolor colorwithhexstring:@"d3d1d1"].    Cgcolor; [Group AddItem:3Widthitem:3num:@[@"Food",@"movie",@"KTV",@"Lobster",@"Dessert",@"Seafood",@"Hot Pot",@"Buffet",@"Vegetable"]]; [Group Setitemblock:^(Nsinteger index) {NSLog (@"%ld", index);    }]; [Self.view Addsubview:group];

Used in 2.cell

Custom Initialize Cell

_group = [[Wjviewgroup alloc]initwithframe:cgrectmake (screenwidth*0.05, Cgrectgetheight (viewbg.frame) +5, screenwidth*0.9, Cellheight)]; _group.isline=YES; if(Cellheight = = the) {_group.heightnum=2; } _group.layer.bordercolor= [Uicolor colorwithhexstring:@"d3d1d1"].        Cgcolor; [_group AddItem:3Widthitem:3num:@[@"",@"",@"",@"",@"",@"",@"",@"",@""]];[Self.contentview Addsubview:_group];

Lazy Loading:

-(Nsarray *) Arraycontext {if (!_arraycontext) {_arraycontext =[[Nsarray alloc]initwithobjects: @[@"Hot pot",@"Cake Dessert",@"Western",@"Buffet",@"Snack food",@"Grilled fish in fragrant pot",@"Dinner and Banquet",@"BBQ Grill",@"Japanese and Korean cuisine"], @[@"Cheap Hotel",@"Luxury hotels",@"Theme Hotel",@"Apartment-style hotels",@"Inn",@"Youth Hostels"], @[@"Hair",@"Nail Nail",@"Beauty body",@"Yoga/Dance"], @[@"Hot spring",@"Ocean Pavilion",@"Exhibition hall",@"Zoological and Botanical Gardens",@"Theme park",@" water park " @[@ " Show event "  board/game  ", @"  sports fitness  ", @"  foot massage  "@ " bath/Sweat steamed ", @ "4d/5d movie  "return _arraycontext;}       

In the Proxy method:

    [Cell.group Item:self.arraycontext[indexpath.row]];    [Cell.group setitemblock:^(nsinteger index) {        NSLog (@ "%ld", Index );            }];

iOS Group Purchase category page (9 Gongge)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.