IOS UITableViewCell Detailed and button click event Processing Instance _ios

Source: Internet
Author: User
Tags uikit

IOS UITableViewCell Detailed and button click event Handling

When I suddenly did my project today, I encountered a problem with the click of the button on the custom UITableViewCell. I know there are two ways, but suddenly can't think of how to do before, good memory is not as bad as written, or record it.

1, the first way to add Tag Value button

Here are two kinds: one is directly in the original UITableViewCell add UIButton button, and then give UIButton set the tag value, and then in the controller in the method through the data, do interface jump and so on. For example, save the memory of half a day.

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {static N 
   
  Sstring *identifier = @ "Cell"; 
  UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:reuseidentifier]; if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:identif 
    IER]; 
  Cell.selectionstyle = Uitableviewcellselectionstylenone; 
  User *user = _users[indexpath.row]; 
  Cell.user = user; 
  Photo button UIButton *photographbutton = [UIButton buttonwithtype:uibuttontypecustom]; 
  Photographbutton.frame = CGRectMake (221, 10, 100, 44); 
  [Photographbutton setimage:[uiimage imagenamed:@ "Camera.png"] forstate:uicontrolstatenormal]; [Photographbutton addtarget:self Action: @selector (photographbuttonclicked:) forControlEvents: 
  UIControlEventTouchUpInside]; 
  Photographbutton.tag = Indexpath.row; 
   
  [Cell.contentview Addsubview:photographbutton]; 
return cell; 
 }

Then click on the event to take the data, add information

-(void) photographbuttonclicked: (UIButton *) sender{ 
   User *user = _users[sender.tag]; 
  Photopickercontroller *photopicker = [[Photopickercontroller alloc] init]; 
  Photopicker.user = user; 
  [Self.navigationcontroller Pushviewcontroller:photopicker animated:yes]; 
   
} 

All two of these methods are in the same controller.

2, custom UITableViewCell, then add an agent method in the UITableViewCell.

#import <UIKit/UIKit.h> @protocol termcelldelegate <NSObject>-(void) Choseterm: (UIButton *) button; 
@end @interface Termcell:uitableviewcell @property (retain, nonatomic) Iboutlet UIButton *checkbutton; 
 
@property (Retain, nonatomic) Iboutlet Uilabel *termlabel; 
@property (Assign, nonatomic) BOOL ischecked; 
 
@property (Assign, nonatomic) id<termcelldelegate> delegate; 
 
-(Ibaction) Checkaction: (UIButton *) sender; @end #import "TermCell.h" @implementation Termcell-(void) awakefromnib {//Initialization code}-(Voi 
 
  d) setselected: (BOOL) selected animated: (bool) animated {[Super setselected:selected animated:animated]; 
  Configure The view for the selected state}-(void) layoutsubviews {[Super layoutsubviews]; if (_ischecked) {[_checkbutton setbackgroundimage:[uiimage imagenamed:@ ' task_state_checked '] ForState:UIControlStat 
  Enormal]; else {[_checkbutton setbackgroundimage:[uiimage imagenamed:@ "task_state_unchecked"] forstate:uicontrolstatenormal]; 
  }-(void) Dealloc {[_checkbutton release]; 
  [_termlabel release]; 
[Super Dealloc]; }-(Ibaction) Checkaction: (UIButton *) Sender {if ([_delegate respondstoselector: @selector (choseterm:)]) {Send 
    Er.tag = Self.tag; 
  [_delegate Choseterm:sender]; 
 }} @end

Then the proxy method of cell can be realized in the controller.

#pragma mark-termcelldelegate 
-(void) Choseterm: (UIButton *) button 
{ 
  _clickindex = Button.tag; 
  Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Are you sure you want to modify the semester? "Message:nil delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" OK ", nil nil]; 
  [Alertview show]; 
} 

Of course, here can also do interface jump, take the data still use the Tag Value button.

Add: Here you can also pass the cell itself back in the proxy method so that you do not have to take data from the array and use the cell's data object directly, simpler yo.

3, is directly in the custom cell inside jumps, this coupling is relatively strong. The idea is to find the parent controller of the button, and then do an interface jump or other action. There is such a tool method

#import "Uiview+additions.h" 
 
@implementation UIView (additions) 
 
-(Uiviewcontroller *) Viewcontroller 
{ 
  Uiresponder *next = [self nextresponder]; 
  Do { 
    if ([Next Iskindofclass:[uiviewcontroller class]]) {return 
      (Uiviewcontroller *) next; 
    } 
     
    Next = [Next Nextresponder]; 
     
  } while (next!= nil); 
   
   
  return nil; 
} 

The header file is not written, it is very simple to expand.

-(void) Setweibomodel: (Weibomodel *) Weibomodel 
{ 
  if (_weibomodel!= weibomodel) { 
    [_weibomodel release]; 
    _weibomodel = [Weibomodel retain]; 
  } 
   
  __block Weibocell *this = self; 
  _userimage.touchblock = ^{ 
    nsstring *nickname = this.weiboModel.user.screen_name; 
    Userviewcontroller *userctrl = [[Userviewcontroller alloc] init]; 
    Userctrl.username = nickname; 
    [This.viewController.navigationController Pushviewcontroller:userctrl Animated:yes]; 
    [Userctrl release];} 
   
 

Here is the assignment model for the cell, and then the Click event is implemented with block.

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.