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

Posted iOS_满聪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于iOS-UITableView-处理cell上按钮事件(弹出警示框,页面跳转等)的主要内容,如果未能解决你的问题,请参考以下文章

swift4.0 cell重用导致ReactiveCocoa(RAC) cell上Button点击事件多次触发问题

IOS-UITableView开发常用各种方法总结

视图交互--表视图(UITableView)的cell交互析略

ios - UITableView 删除按钮不会对手势做出反应

UITableviewCell上添加button,按钮的normal或selected状态显示不正常的可能原因。

一个最简单的cell按钮点击回调