zoukankan      html  css  js  c++  java
  • 【原】自定义tableviewcell中多个button点击实现不同功能

    #import <UIKit/UIKit.h>
    
    @protocol TableCellDelegate <NSObject>
    
    - (void)choseTerm:(UIButton *)button;
    
    @end
    
    @interface TableViewCell : UITableViewCell
    
    @property (weak, nonatomic) IBOutlet UIButton *checkButton;
    @property (weak, nonatomic) IBOutlet UIButton *checkButton2;
    @property (weak, nonatomic) IBOutlet UILabel *label;
    
    @property (assign, nonatomic) BOOL  isChecked;
    @property (assign, nonatomic) id<TableCellDelegate> delegate;
    
    - (IBAction)checkAction:(UIButton *)sender;
    - (IBAction)checkAction2:(UIButton *)sender;
    

    .m文件

    #import "TableViewCell.h"
    
    @implementation TableViewCell
    
    - (void)awakeFromNib {
        // Initialization code
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }
    
    - (IBAction)checkAction:(UIButton *)sender {
        if ([_delegate respondsToSelector:@selector(choseTerm:)]) {
            sender.tag = self.checkButton.tag;
            NSLog(@"tag:%ld",self.checkButton.tag);
            [_delegate choseTerm:sender];
        }
    }
    
    - (IBAction)checkAction2:(UIButton *)sender {
        if ([_delegate respondsToSelector:@selector(choseTerm:)]) {
            sender.tag = self.checkButton2.tag;
            NSLog(@"tag2:%ld",self.checkButton2.tag);
            [_delegate choseTerm:sender];
        }
    }
    @end
    

     viewcontroller.m

    #import "ViewController.h"
    #import "TableViewCell.h"
    
    @interface ViewController ()<UITableViewDelegate, UITableViewDelegate, TableCellDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 5;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *cellIdentifier = @"Cell";
        static BOOL nibsRegistered = NO;
        if (!nibsRegistered) {
            UINib *nib = [UINib nibWithNibName:@"TableViewCell" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
            nibsRegistered = YES;
        }
        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        cell.delegate = self;
        
        cell.checkButton.tag = indexPath.row*1000+1;
        cell.checkButton2.tag = indexPath.row*1000+2;
        NSLog(@"checkButton.tag:%ld;checkButton2.tag=%ld",cell.checkButton.tag,cell.checkButton2.tag);
        cell.label.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
        return cell;
    }
    
    - (void)choseTerm:(UIButton *)button
    {
        _clickIndex = button.tag;
        if (_clickIndex%1000 == 1) {
            NSLog(@"第一列!");
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"确定修改学期吗?" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil,nil];
            [alertView show];
        }else {
            NSLog(@"第二列!");
        }
    }
    
    @end
    
  • 相关阅读:
    BZOJ 2219 数论之神 (CRT推论+BSGS+原根指标)
    BZOJ 2618: [Cqoi2006]凸多边形 (半平面交)
    BZOJ 1038: [ZJOI2008]瞭望塔
    BZOJ 1007: [HNOI2008]水平可见直线 (半平面交)
    BZOJ 1845: [Cqoi2005] 三角形面积并 (辛普森积分)
    BZOJ 2458: [BeiJing2011]最小三角形 (分治)
    BZOJ 3210: 花神的浇花集会 (切比雪夫距离)
    BZOJ 2013 : [Ceoi2010]A huge tower / Luogu SP6950 CTOI10D3
    BZOJ 3630: [JLOI2014]镜面通道 (网络流 +计算几何)
    bzoj 2820 YY的GCD
  • 原文地址:https://www.cnblogs.com/saurik/p/4896069.html
Copyright © 2011-2022 走看看