• iOS-UITableView-处理cell上按钮事件(弹出警示框,页面跳转等)


    一. 目的:

    实现UITableViewCell上按钮点击事件可以进行页面跳转.

    二. 实现方法:

        1. 用协议的方式的实现.

        2. 需要自定义UITableViewCell.

    三. 代码部分.

    cell.h中

    #import <UIKit/UIKit.h>
    
    @protocol SevenProtocolDelegate <NSObject>
    
    - (void)sevenProrocolMethod:(UIViewController *)viewController and:(NSInteger)cellRow;
    
    @end
    
    @interface SevenCell : UITableViewCell
    
    @property (nonatomic, weak) id<SevenProtocolDelegate> customDelegate;
    
    @property (nonatomic, strong) UIViewController  * viewController;
    @property (nonatomic, assign) NSInteger  cellRow;
    
    
    @end

    cell.m中

    #import "SevenCell.h"
    
    #import "Masonry.h"
    
    @interface SevenCell ()
    
    @property (nonatomic, strong) UIView    * bgView;
    @property (nonatomic, strong) UIButton  * button;
    
    @end
    
    
    @implementation SevenCell
    
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
        {
            [self addSubviews];
        }
        return self;
    }
    
    - (void)addSubviews
    {
        [self addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self).with.insets(UIEdgeInsetsMake(0, 0, 10, 0));
        }];
        
        [self.bgView addSubview:self.button];
        [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.bgView).with.offset(10);
            make.top.mas_equalTo(self.bgView).with.offset(10);
            make.size.mas_equalTo(CGSizeMake(200, 40));
        }];
    }
    
    - (void)buttonClicked
    {
        if (self.customDelegate != nil && [self.customDelegate respondsToSelector:@selector(sevenProrocolMethod:and:)])
        {
            [self.customDelegate sevenProrocolMethod:self.viewController and:self.cellRow];
        }
    }
    
    #pragma mark - 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点击事件跳转下一个页面" forState:UIControlStateNormal];
            [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
        }
        return _button;
    }
    
    @end

    controller.m中

    #import "SevenViewController.h"
    #import "SevenCell.h"
    
    #import "Seven_oneViewController.h"
    
    #import "Masonry.h"
    
    @interface SevenViewController () <UITableViewDelegate,UITableViewDataSource,SevenProtocolDelegate>
    
    @property (nonatomic, strong) UITableView * tableView;
    
    @end
    
    @implementation SevenViewController
    
    #pragma mark - 生命周期
    #pragma mark viewDidLoad
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        [self basicSetting];
        
        [self addTableView];
    }
    
    
    #pragma mark - 系统代理
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 5;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 80;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString * 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;
        
        return cell;
    }
    
    #pragma mark - 点击事件
    
    #pragma mark - 实现方法
    
    #pragma mark - 自定义协议
    - (void)sevenProrocolMethod:(UIViewController *)viewController and:(NSInteger)cellRow
    {
        // 可以通过 cellRow 区分哪个cell上的button跳转对应的页面
        
        if (cellRow == 0 || cellRow == 1)
        {
            Seven_oneViewController * seven_one = [[Seven_oneViewController alloc] init];
            
            [self.navigationController pushViewController:seven_one animated:YES];
        }
    }
    
    #pragma mark 基本设置
    - (void)basicSetting
    {
        self.title = @"cell上button页面跳转";
    }
    
    - (void)addTableView
    {
        [self.view addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 0, 0, 0));
        }];
    }
    
    #pragma mark - 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
    你的一次推荐就是对我莫大的支持。感觉不错,给个推荐或者评论吧。
  • 相关阅读:
    概率期望小记
    洛谷P5591 小猪佩奇学数学【单位根反演】
    EasyUI取消树节点选中
    EasyUI获取正在编辑状态行的索引
    js判断是否是大小写,数字等方法
    ElasticSearch部署问题
    全文检索ES 服务启动和关闭
    文件异步上传
    js控制时间显示格式
    SpringMVC 多视图解析器 跳转问题
  • 原文地址:https://www.cnblogs.com/mancong/p/5585728.html
Copyright © 2020-2023  润新知