Cell 上的多個按鈕點擊事件處理

一、第一種方式給Button加上tag值ide

這裏分爲兩種:一種是直接在原生的UITableViewCell上添加UIButton按鈕,而後給UIButton設置tag值,而後在控制器裏的方法裏經過取數據,作界面跳轉等。仍是舉個例子吧,省的回憶半天。工具

?atom

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
       
     static  NSString *identifier = @ "Cell" ;  
       
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];  
     if  (cell == nil) {  
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];  
         cell.selectionStyle = UITableViewCellSelectionStyleNone;  
     }  
      User *user = _users[indexPath.row];  
     cell.user = user;  
     //拍照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;  
}


而後在點擊事件中取數據,加信息spa

?.net

1
2
3
4
5
6
7
- ( void )photographButtonClicked:(UIButton *)sender{  
      User *user = _users[sender.tag];  
     PhotoPickerController *photoPicker = [[PhotoPickerController alloc] init];  
     photoPicker.user = user;  
     [self.navigationController pushViewController:photoPicker animated:YES];  
       
}

以上兩個方法都是在同一個控制器中。另一種,自定義了UITableViewCell,那麼就在UITableViewCell裏添加一個代理方法。代理

?code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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  
}  
   
- ( void )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:UIControlStateNormal];  
     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:)]) {  
         sender.tag = self.tag;  
         [_delegate choseTerm:sender];  
     }  
}  
   
@end

而後再控制器中實現Cell的代理方法便可orm

?blog

1
2
3
4
5
6
7
#pragma mark - TermCellDelegate  
- ( void )choseTerm:(UIButton *)button  
{  
     _clickIndex = button.tag;  
     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@ "肯定修改學期嗎?"  message:nil delegate:self cancelButtonTitle:@ "取消"  otherButtonTitles:@ "肯定" , nil nil];  
     [alertView show];  
}

固然,這裏也能夠作界面跳轉,取數據依然用button的tag值。第二種,是直接在自定義的Cell裏面跳轉,這種耦合性比較強。思路先是找到button的父控制器,而後作界面跳轉或者其餘操做。有這樣一個工具方法事件

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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;  
}
相關文章
相關標籤/搜索