ios-uitableview-Handling button events on the cell (Popup alert box, page jump, etc.)

Source: Internet
Author: User

I. Purpose:

Implement UITableViewCell on the button click event can be page jump.

Two. Implementation method:

1. Implementation of the Protocol.

2. Custom UITableViewCell is required.

Three. Code section.

In Cell.h

#import <UIKit/UIKit.h>@protocol sevenprotocoldelegate <nsobject>-(void) Sevenprorocolmethod: (Uiviewcontroller *) Viewcontroller and: (Nsinteger) Cellrow; @end @interface  ID<SevenProtocolDelegate> customdelegate; @property (nonatomic, Strong) Uiviewcontroller  * viewcontroller; @property (nonatomic, assign) Nsinteger  Cellrow; @end

In CELL.M

#import "SevenCell.h"#import "Masonry.h"@interfaceSevencell () @property (nonatomic, strong) UIView*Bgview, @property (nonatomic, strong) UIButton*button;@end@implementationSevencell-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{if(self =[Super Initwithstyle:style Reuseidentifier:reuseidentifier])    {[Self addsubviews]; }    returnSelf ;}- (void) addsubviews{[self addSubview:self.bgView]; [Self.bgview mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.edges.mas_equalTo (self). With.insets (Uiedgeinsetsmake (0,0,Ten,0));        }];    [Self.bgview AddSubview:self.button]; [Self.button mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.left.mas_equalTo (Self.bgview). With.offset (Ten); Make.top.mas_equalTo (Self.bgview). With.offset (Ten); Make.size.mas_equalTo (Cgsizemake ( $, +)); }];}- (void) buttonclicked{if(Self.customdelegate! = Nil &&[self.customdelegate respondstoselector: @selector (Sevenprorocolmethod:and:)]) {[Self.customdelegate SevenP    RorocolMethod:self.viewController And:self.cellRow]; }}#pragmaMark-setter & Getter-(UIView *) bgview{if(!_bgview) {Self.bgview=[[UIView alloc] init]; Self.bgView.backgroundColor=[Uicolor Whitecolor]; }    return_bgview;}-(UIButton *) button{if(!_button) {Self.button=[UIButton Buttonwithtype:uibuttontypecustom]; Self.button.layer.masksToBounds=YES; Self.button.layer.cornerRadius=20.0f; Self.button.backgroundColor=[Uicolor Orangecolor]; Self.button.titleLabel.font= [Uifont boldsystemfontofsize:14.0f]; [Self.button settitle:@"button click event to jump to next page"Forstate:uicontrolstatenormal];        [Self.button Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];    [Self.button addtarget:self Action: @selector (buttonclicked) forcontrolevents:uicontroleventtouchupinside]; }    return_button;}@end

In CONTROLLER.M

#import "SevenViewController.h"#import "SevenCell.h"#import "Seven_oneviewcontroller.h"#import "Masonry.h"@interfaceSevenviewcontroller () <UITableViewDelegate,UITableViewDataSource,SevenProtocolDelegate>@property (nonatomic, strong) UITableView*TableView;@end@implementationSevenviewcontroller#pragmaMark-Life cycle#pragmaMark Viewdidload-(void) viewdidload{[Super Viewdidload];        [Self basicsetting]; [Self Addtableview];}#pragmaMark-System Agent-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return 5;}-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return  the;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString * identifier =@"Cell"; Sevencell* Cell =[TableView Dequeuereusablecellwithidentifier:identifier]; if(!cell) {Cell=[[Sevencell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:identifier]; } cell.customdelegate=Self ; Cell.viewcontroller=Self ; Cell.cellrow=Indexpath.row; returncell;}#pragmaMark-click events#pragmaMark-Implementation Method#pragmaMark-Custom Protocol-(void) Sevenprorocolmethod: (Uiviewcontroller *) Viewcontroller and: (Nsinteger) cellrow{//you can distinguish which cell button jumps to the corresponding page by Cellrow        if(Cellrow = =0|| Cellrow = =1) {Seven_oneviewcontroller* Seven_one =[[Seven_oneviewcontroller alloc] init];    [Self.navigationcontroller Pushviewcontroller:seven_one Animated:yes]; }}#pragmaMark Basic Settings-(void) basicsetting{Self.title=@"on the Cell button page Jump";}- (void) addtableview{[Self.view AddSubview:self.tableView]; [Self.tableview mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.edges.mas_equalTo (Self.view). With.insets (Uiedgeinsetsmake (Ten,0,0,0)); }];}#pragmaMark-setter & getter-(UITableView *) tableview{if(!_tableview) {Self.tableview=[[UITableView alloc] Initwithframe:cgrectzero Style:uitableviewstyleplain]; Self.tableview.Delegate=Self ; Self.tableView.dataSource=Self ; }    return_tableview;}@end

ios-uitableview-Handling button events on the cell (Popup alert box, page jump, etc.)

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.