OC implementation of radio and multi-select buttons

Source: Internet
Author: User

This code library temporarily has OC package, another day to fill a swift package, mainly because Swift is not so familiar, afraid of error, half a month to find the problem more embarrassing!

Attach democsdn:http://download.csdn.net/detail/qq_34863867/9593700 First

GitHub Address: Https://github.com/SZLCode/RadioAndCheck

First, upload two cards:

The two are basically similar, only part of the logic is different, the bottom of the radio button as an example, analysis of the idea:

Radio button mainly consists of two parts, one is a single choice of each row and the title two parts, options have front button and the text behind the two parts.

Start the package below, first encapsulate the option line, just analyzed the line is composed of these two parts, first start to build this part, create Radiobuttoncell:

-(Instancetype) Initradiocell: (NSString *) celltitle selectimg: (NSString *) seletimg unselectimg: (NSString*) Unselectimg Cellsizeblock: (void (^) (cgsize)) callbacksize{if (self=[super init]) {self.selectimagename = Seleti        mg        Self.unselectimagename = unselectimg;        Self.celltitle = Celltitle;        [Self makecellview];        Cgsize size = Cgsizemake (Screen_width, _labelview.frameheight);        _tapview.frameheight = Size.Height;    Callbacksize (size); } return self;}    Cell contents-(void) makecellview{_tapview = [UIView new];    _tapview.frame = CGRectMake (content_margin, 0, screen_width-content_margin*2, 35);    [Self addsubview:_tapview];    The button is not used here because you want to achieve a click anywhere on each line can be marked red, instead of just clicking the button part _btnview = [Uiimageview new];    _btnview.frame = CGRectMake (0, 3, 24, 24);    _btnview.image = [UIImage imageNamed:self.unSelectImageName];        [_tapview Addsubview:_btnview];    CGFloat cellhight =[uilabel getHeightWithTitle:self.celltitle font:16]; _labelview = [VeRtlabel new];    _labelview.text = Self.celltitle;    _labelview.font = Font (labelfont);    _labelview.numberoflines = 0;    [_labelview SizeToFit]; _labelview.frame = CGRectMake (_btnview.frameright+content_margin, 0, _tapview.frameright-(_btnView.frameRight+    Content_margin), cellhight+content_margin*2); [_tapview Addsubview:_labelview];}

  

interface to this build completed, analysis of the cell every click once, cell selected state will change, so create a click method to change the selected state

Click the button row to determine whether to select or uncheck-(void) tapradiocell{    if (self.tapstate) {[Self Cancelselectedradiocell], depending on the selected state        ;    } else{        [self Selectradiocell];    }    Self.tapstate =!self.tapstate;} The identity is selected #pragma can be changed here after the label is not marked red, the default is red-(void) selectradiocell{    _btnview.image = [UIImage imagenamed: Self.selectimagename];    _labelview.textcolor = [Uicolor redcolor];} Unchecked state-(void) cancelselectedradiocell{    _btnview.image = [UIImage imageNamed:self.unSelectImageName];    _labelview.textcolor = [Uicolor blackcolor];}

At this point, the work that needs to be done in the cell is complete.

Create a radio button class RadioButton, all control of the state and content of Radiobuttoncell comes here.

Create a cell based on the number of incoming cells

Create cell View-(void) makeradiocell{        __block cgfloat cellinithigh = _labeltitle.framebottom;//Sets the starting position for the cell height for    (int i=0;i<self.labelstringarr.count;i++) {                __block Radiobuttoncell * cell =  [[Radiobuttoncell alloc] Initwithframe:cgrectmake (0, Cellinithigh, Screen_ WIDTH, +)];        cell = [cell initradiocell:self.labelstringarr[i] SelectImg:self.selectImageName unselectImg:self.unSelectImageName cellsizeblock:^ (cgsize size) {            Cellinithigh + = size.height;            Cell.frameheight = Size.Height;        }];        Cell.tag = 60000+i;        [Self Addsubview:cell];                UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (tapcellaction:)];        tap.numberoftapsrequired = 1;        [Cell Addgesturerecognizer:tap];}    }

  

After clicking on the cell area, according to the cell's original state to make changes: There are three types of radio status: The original is not selected, select the click of the cell, there is the original selected cell, it is necessary to determine the latest click of the cell is the original selected cell, is to cancel the original selected If it is not the original selected, select the latest cell.

After clicking Cell-(void) Tapcellaction: (Uigesturerecognizer *) tap{nsinteger tag = Tap.view.tag;        if (HAVETAP) {//There is already a selected option Radiobuttoncell * cell = [self viewwithtag:tag];            Determine whether the selected one would otherwise be selected if (tag==indexold) {//is the original selected row [cell tapradiocell];//unchecked havetap = NO;            Indexold = 59999;        [Self.delegate Selectradiobuttonindex:indexold];            }else{//is not the original selected line Radiobuttoncell *cellold = [self viewwithtag:indexold]; [Cellold tapradiocell];//cancels the originally selected row [cell tapradiocell];//select New Row [Self.delegate Selectradiob            UTTONINDEX:TAG-60000];            Havetap = YES;        Indexold = tag;        }}else{//does not exist the option that has been selected Radiobuttoncell * cell = [self viewwithtag:tag];        [Cell Tapradiocell];        if (cell.tapstate) {//Select a row state, return the selected row [Self.delegate selectradiobuttonindex:tag-60000];    } indexold =tag;//Record the selected button line Havetap = YES; }    }

Defines a proxy method that returns the cell to which the user clicked

-(void) Selectradiobuttonindex: (Nsinteger) index;

  

All that is done is to create and implement the Proxy method where you need to create the radio button.

-(void) awakefromnib{        RadioButton *radiobutton = [[RadioButton alloc]initwithframe:cgrectmake (0, 414, 736)];        Radiobutton.delegate = self;        RadioButton = [RadioButton initradiobuttontitle:@ "Please evaluate good or bad" Selectimgname:nil unselectimgname:nil labelarr:@[@ "OK", @ " BBBBBBBBBBBBBBBBBBBBB ", @" This is the third option, this is the third option, you see I can automatically wrap yo! Don't you believe me? Look, I'll show you the line! "]];            [Self.view Addsubview:radiobutton];} #pragma radiodelegate-(void) Selectradiobuttonindex: (nsinteger) index{    if (index==59999) {        NSLog (@ "no options");    } else{        Nsarray * arr = @[@ "AAA", @ "BBB", @ "CCC", @ "DDD", @ "EEE", @ "FFF", @ "GGG", @ "HHH", @ "III"];        NSLog (@ "I am%@", Arr[index]);    }  }

At this point, all the radio buttons work.

The multi-select button is the same as this page, but the logic is slightly different in the selected place.

If the cell clicked is not selected, the cell is selected, and if the cell is selected, it will be removed from the selected state.

After clicking Cell-(void) Tapcellaction: (Uigesturerecognizer *) tap{    Checkbuttoncell * cell = [Self Viewwithtag: Tap.view.tag];    [Cell Tapcheckcell];    NSString *indexstr = [NSString stringwithformat:@ "%ld", tap.view.tag-70000];    if (cell.tapstate) {//Select a row state        [Self.selectindexarr addobject:indexstr];    } else{//Cancel the selected state of a row        [Self.selectindexarr removeobject:indexstr];    }    [Self.delegate SELECTCHECKBUTTONINDEX:SELF.SELECTINDEXARR];}

Then use the proxy to return the latest user-selected list in an array form

-(void) Selectcheckbuttonindex: (nsmutablearray*) Indexarr;

and create it where it's shown.

-(void) awakefromnib{        Checkbutton *checkbtn = [[Checkbutton alloc]initwithframe:cgrectmake (0, 414, 736)];    Checkbtn.delegate = self;    CHECKBTN = [checkbtn initcheckbuttontitle:nil selectimgname:nil unselectimgname:nil labelArr:@[@ "AAAAAAAAAAAAAAAAAAA ", @" bbbbbbbbbbbbb ", @" This is multi-option Oh, this is multi-option Oh, multi-options Oh, options oh, item Oh, Oh! , @ "ddddddddddddddd", @ "eeeeeeeeeeeeee", @ "This is the last item, I can continue to add a lot of options oh, do not believe you can add GGGGGGGGGGGG to try it!" "]];        [Self.view addsubview:checkbtn];} #pragma checkdelegate-(void) Selectcheckbuttonindex: (Nsmutablearray *) indexarr{    if (indexarr.count) {        Nsarray * arr = @[@ "AAA", @ "BBB", @ "CCC", @ "DDD", @ "EEE", @ "FFF", @ "GGG", @ "HHH", @ "III"];        for (int i=0; i<indexarr.count; i++) {            Nsinteger index = [indexarr[i] integervalue];            NSLog (@ "I am%@ option", Arr[index]);        }    } else{        NSLog (@ "No options! ");    }}

Here, the library of radio and multi-select buttons is created and used to complete.

OC implementation of radio and multi-select buttons

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.