Block在UITableViewcell中的正向和反向传值

iOS UITableView配合block 回调实现列表删除教程 前言: 各位同学大家好 ,有段时间没有给大家更新文章了 。 具体多久我也记不清楚了,春节放假比较早,所以就趁着有时间学习了一下iOS开发的基础知识 今天讲的iOS UITableView 配和block 回调实现列表删除教程 。那么废话不多说 ,我们正式开始。 准备工作 安装xcode 这个大家可以自己去appstore 搜索下载安装即可 效果图 具体实现 列表显示 @interface ViewController ()<UITableViewDelegate,UITableViewData 阅读详情

我们都习惯说 反向传值用Block,但是正向和反向都只是相对的

下面我 先 用一个自定义cell来给button“正向”传值,效果如下图(带颜色的是重点哦)

1、“正向”传值









首先创建 UITableView

<h2>#pragma mark*******创建UITableView*******</h2>-(void)createTableView{
    
    UITableView *table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style: UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    
    
}
<h2>#pragma mark*******设置其它参数************</h2><pre name="code" class="objc">- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 1;
    
}


#pragma mark*******下面是两个必须实现的UITableView 方法******

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return 200;
    }
    return 100;
    
}

//*******看case 0:就行了******

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.<span style="color:#3333ff;">section </span>) {
        <span style="color:#ff0000;">case 0:</span>
        {
            
            NSString *messageID = @"ID";
            <span style="color:#ff0000;">Practice_TableViewCell</span> *cell = [tableView <span style="color:#3333ff;">dequeueReusableCellWithIdentifier</span>:messageID];
            if (!cell) {
                 cell = [[<span style="color:#ff0000;">Practice_TableViewCell</span> <span style="color:#3333ff;">alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier</span>:messageID];
                
        }
           
            cell.<span style="color:#3366ff;">backgroundColor = [UIColor blueColor]</span>;
            cell.<span style="color:#3366ff;">selectionStyle = UITableViewCellSelectionStyleNone</span>;
            <span style="color:#ff0000;">return</span> cell;
            
        }
            break;
            
            case 1:
        {
            NSString *messageID = @"IDD";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:messageID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
            }
            cell.backgroundColor = [UIColor blueColor];
            
            
            return cell;
            
        }
            break;
            
            case 2:
        {
            NSString *messageID = @"IDDD";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:messageID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
            }
            cell.backgroundColor = [UIColor blueColor];
            
            return cell;
        }
            break;
            
        default:
            break;
    }
    
    
    return nil;
}

下面是对cell的重写

Practice_TableViewCell.h
<pre name="code" class="objc">#import <UIKit/UIKit.h>

@interface Practice_TableViewCell : UITableViewCell

<h1>// 定义Block</h1><h3><span style="color:#ff0000;">@property(nonatomic,copy)void(^buttonBlock)(</span><span style="color:#3366ff;">UIButton *button , UITableViewCell *cell</span><span style="color:#ff0000;">);//这里很重要,一会正向传button的
</span>这里我为什么还在Block中定义 cell呢?这样理解吧,因为是自定义cell,目的是在cell上添加其它东西,所以得<span style="color:#3333ff;">把cell和要添加的东西写在一起</span>啊</h3>

@end



Practice_TableViewCell.m
@implementation Practice_TableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        for (int i=0; i<2; i++) {
            
            UIButton *buttonxx = [UIButton buttonWithType:UIButtonTypeCustom];
            buttonxx.frame = CGRectMake(50+(100+50)*i, 50, 100, 100);
            buttonxx.layer.cornerRadius = 50;
            buttonxx.tag = 10+i;
            [buttonxx addTarget:self action:@selector(btnBlock:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:buttonxx];
            
    }
        
            UIButton *button00 = (UIButton *)[self.contentView viewWithTag:10];
            button00.backgroundColor = [UIColor redColor];
            [button00 setTitle:@"我要变" forState:UIControlStateNormal];
            UIButton *button01 = (UIButton *)[self.contentView viewWithTag:11];
            button01.backgroundColor = [UIColor yellowColor];
    }
    
    
    
            return self;
}

-(void)btnBlock:(UIButton *)sender{
    
    NSLog(@"点击的是%ld",sender.tag);
    
    
<h3>    <span style="color:#ff0000;">self.buttonBlock(</span><span style="color:#3333ff;">sender, self</span><span style="color:#ff0000;">);//这里是在点击button的时候传值,另一个目的也是让它能在cell上响应</span></h3>    
    
}

下面我们就在上面的viewController中实现Block,也就是刚才让 看case :0的那里,插入的代码如下:有颜色的部分就是对Block的实现

<span style="white-space:pre">	</span>    if (!cell) {
                cell = [[LianXi_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
                
            }
            
            
<h3>            <span style="color:#ff0000;">cell.buttonBlock = ^(UIButton *button , UITableViewCell *cell){</span></h3>                
                <span style="color:#3333ff;">switch (button.tag) {
                    case </span><span style="color:#ff0000;">10</span><span style="color:#3333ff;">:
                    {
                        Next_ViewController *next = [[Next_ViewController alloc]init];
                        [self.navigationController pushViewController:next animated:YES];
    
                    }
                        break;
                        
                        case </span><span style="color:#ff0000;">11</span><span style="color:#3333ff;">:
                    {
                        //这里暂时不写
                        
                    }
                        break;
                        
                    default:
                        break;
                }
                </span>
<h3>            <span style="color:#ff0000;">};</span></h3>            cell.backgroundColor = [UIColor blueColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }

运行之后点击红色button就可以跳转了,这里既解释了自定义cell,又解释了Block的"正向"传值

综上所述就相当于"正向"传值


2、“反向”传值
下面我在这个的基础上 进行 “反向” 传值,这是第二个页面Next_ViewController
<span style="font-size:24px;">Next_ViewController.h</span>
#import <UIKit/UIKit.h>

@interface Next_ViewController : UIViewController

<span style="font-size:24px;color:#ff0000;">@property(nonatomic, copy)void(^imageJumpBlock)(</span><span style="font-size:24px;color:#3333ff;">UIButton *imageJump</span><span style="font-size:24px;color:#ff0000;">);//定义</span>

@end

<span style="font-size:24px;">Next_ViewController.m</span>
#import "Next_ViewController.h"
#import "ViewController.h"


@interface Next_ViewController ()
{
    
    UIButton *buttonModel;
    
}
@end

@implementation Next_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *labelChange = [[UILabel alloc]initWithFrame:CGRectMake(20, 250, CGRectGetWidth([UIScreen mainScreen].bounds)-40, 50)];
    labelChange.backgroundColor = [UIColor redColor];
    labelChange.text = @"你猜我变了吗?";
    labelChange.textAlignment = NSTextAlignmentCenter;
    labelChange.font = [UIFont systemFontOfSize:45];
    [self.view addSubview:labelChange];
    
   <span style="color:#3333ff;"> </span><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;"> = [</span><span style="color:#ff0000;">UIButton</span><span style="color:#3333ff;"> buttonWithType:UIButtonTypeCustom];
   </span><span style="background-color: rgb(255, 255, 255);"><span style="color:#ff0000;"> buttonModel</span></span><span style="color:#3333ff;">.frame = CGRectMake(100, 150, CGRectGetWidth([UIScreen mainScreen].bounds)-200, 50);
    </span><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;">.backgroundColor = [UIColor redColor];
    [</span><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;"> setTitle:@"返回" forState:UIControlStateNormal];
    [</span><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;"> addTarget:self action:@selector(returnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:buttonModel];</span>
    
    
}

-(void)returnAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
   <span style="color:#3333ff;"> <span style="font-size:24px;">self.imageJumpBlock(</span></span><span style="font-size:24px;"><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;">);//</span><span style="color:#ff0000;">这是第二个页面传button的地方</span></span>
    
    
}

ViewController这里是第一个页面:实现的地方,依然是看case:0里面,我在刚才的Block的里面又嵌套了一个 Block,虽然这里看着像嵌套,但是是在switch里面实现的,是,这里的实现是为了 跳转到第二个页面Next_viewController的瞬间,就从那里把

buttonModel传到ViewController 的next.imageJumpBlock = ^(UIButton *imageJum){实现};里面,但是为了看到传值的效果,我把

<span style="color:#3333ff;"> <span style="font-size:24px;">self.imageJumpBlock(</span></span><span style="font-size:24px;"><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;">);放到了button的触发方法里面</span></span>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section ) {
        case 0:
        {
            
            NSString *messageID = @"ID";
            LianXi_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:messageID];
            if (!cell) {
                cell = [[LianXi_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
                
            }
            
            
           <span style="color:#3333ff;"> cell.buttonBlock = ^(UIButton *button , UITableViewCell *cell){
                
                switch (button.tag) {
                    case 10:
                    {
                        Next_ViewController *next = [[Next_ViewController alloc]init];
                        [self.navigationController pushViewController:next animated:YES];</span>
<span style="color:#3333ff;">                       </span><span style="font-size:18px;color:#ff0000;"> next.imageJumpBlock = ^(UIButton *imageJum){
                        
                            </span><span style="font-size:18px;color:#3333ff;">[button setBackgroundImage:[UIImage imageNamed:</span><span style="font-size:18px;color:#ff0000;">@"000"</span><span style="font-size:18px;color:#3333ff;">] forState:UIControlStateNormal];</span><span style="font-size:18px;color:#ff0000;">
                        </span><span style="font-size:24px;color:#3333ff;">记住</span><span style="font-size:24px;color:#ff0000;">@“000”</span><span style="font-size:24px;color:#3333ff;">我这里是给button 添加的一张图片,不要忘了<span style="font-family: Arial, Helvetica, sans-serif;">    </span></span><span style="font-size:18px;color:#ff0000;">
                        };</span><span style="font-size:24px;color:#ff0000;">//这括号里面的就是第二个页面 传过来的值 用来实现的</span><span style="color:#3333ff;">
    
                    }
                        break;</span>
                        
                        case 11:
                    {
                        //这里暂时不写
                        
                    }
                        break;
                        
                    default:
                        break;
                }
                
            };
            cell.backgroundColor = [UIColor blueColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }
            break;

运行之后点击红色button


调到的这个页面是Next_viewController,为了看到效果你可以先点 <Back 反回去看红色button变没变,然后再回到Next_viewController点击      返回       这样就会很明显的看到反向传值的效果了

大家可以自己对比一下 “正向” 和 “反向” 它们传button的地方 和 实现的地方的不同

我是菜鸟,如果哪里写得不对,希望大家批评指正












iOS 中自定义cell和控制器之间常用传值方式 iOS中常用传值方式浅析 阅读详情

相关推荐

网络攻防实验——网络监听

利用CAIN实现ARP监听。

Trieyes_of_feng的博客 3197

iOS开发 UI 正向传值 反向传值,Block传值

正向传值 (属性传值)将A界面的值通过属性赋值给B界面 主要分三步: 准备工作: 建立两个控制器FirstViewcontroller,SecondViewcontroller,FirstView,SecondView 在FirstView中 #import @interface FirstView : UIView @property (nonatomic,stro

yx920014615的博客 1844

前端实现微信扫一扫的思路

本篇文章的关键点就是讲解了我在实现处理多张二维码的场景时的思路,利用canvas遮挡识别过的二维码这个思路是pbk-bin大佬最先想到的,在实现这个需求以后还是很感叹这个思路的巧妙。👏pbk-bin🎁~如果文章对你有帮助,不妨赠人玫瑰,手有余香,预计将会在下篇更新较为完整的微信扫一扫界面和功能。

fang_my的博客 2492

解决UITableViewCell重用导致Block回调参数不正确的问题

解决UITableViewCell重用导致Block回调参数不正确的问题

DabbyC的博客 679

美团外卖mtgsig 1.2逆向工程:从补环境到签名生成全解析

本文深入解析了美团外卖mtgsig 1.2签名算法的逆向工程全流程。从环境检测原理与补环境实战入手,详细拆解了签名JSON中每个关键字段的生成逻辑,并重点剖析了核心签名a2的生成步骤,涉及设备指纹、MD5、AES加密等关键技术。最后提供了构建签名生成器的实践思路与常见坑点,为理解复杂风控体系提供了完整的技术视角。

weixin_29324401的博客 1764

【声纳与人工智能融合——从理论前沿到自主系统实战】第一章 声纳AI的数据基础:仿真、数据集与表征学习

该数据集的特点是包含了多种底质背景(沙地、泥地、碎石区)与噪声条件,适合验证模型的背景抑制能力。Urashima数据集由日本海洋研究开发机构(JAMSTEC)发布,基于自主水下航行器(AUV)采集,包含前视声纳序列数据,主要用于水下导航与避障算法评估,其数据包含了精确的姿态与深度标注,支持三维场景重建算法的训练。北约海洋研究与实验中心(CMRE)发布的CMRE datasets包含侧扫与合成孔径声纳数据,特别适用于跨传感器域自适应研究,数据集中包含了同一区域的多种声纳成像结果,为跨模态学习提供了基准。

u012133341的博客 104

手把手教你在TableView的cell里使用Block写一个点击事件

前言 学习MVC之后button点击事件成为心中永远的痛,协议是又臭又长还容易写错,到了cell的点击事件更是一筹莫展 所以,还是早点学会block点击事件,开始愉快的用block写所有的点击事件吧 本文主要介绍在UITableViewCell里如何使用Block写点击事件 参考文章 iOS开发–Block传递按钮点击事件 一篇文章看懂iOS代码块Block iOS 开发:『Blocks』详...

KevinAshen的博客 1093

安卓逆向 | 某社交类APP gsid

*本文章仅供学习交流,切勿用于非法通途,如有侵犯贵司请及时联系删除

爬虫进击之路 3086

iOS高级理论:Block的应用

Block 是 Objective-C 和 Swift 中的一种语言特性,可以用来封装一段代码并在需要时执行。

长沙火山 1215

Windows 11 安装 Miniconda + Mamba,配置国内源

本文详细介绍了在 Windows 11 系统下安装 Miniconda 并配置 Mamba 的完整流程,包括选择精简版 Miniconda 的理由、安装过程中的关键选项设置、配置清华大学镜像源以加速包下载,以及在 base 环境中设置 pip 国内源。同时说明了 Mamba 相较 Conda 的性能优势,并指导用户完成 Mamba 安装与 Shell 集成,最终通过命令验证环境管理功能,帮助用户构建高效、轻量的 Python 开发环境。

寒水馨 · 轻量级技术文档 · hanshuixin.org 3348

【WRF-Chem教程第四期上】运行 WRF-Chem 模型

完整介绍运行 WRF-Chem 模型的详细步骤

WW、forever的博客 3117

IOS菜鸟的所感所思——UITableView中数据之间的顺序与反向传递

这是我的第一篇关于IOS的文章,可能有些不对的地方,希望大家指正。 步骤: 一: 1.建立一个项目,其ViewController的类型是UITableViewController,设置其identifier——MyTextCell;(点击viewcontroller的控制器后 点击上面的标题栏Editor中的embed in中的Navigation) 2.添加一个新的controller

MyCodeDancer的博客 1925

ZYNQ系列PS侧和PL侧DDR3或DDR4在AD(Altium Designer 25)中的layout布线规则约束

DDR3或DDR4的PCB布线规则通常有相对固定的要求,对应的,在PCB设计时有固定的约束设置。

FPGA艺术家的博客 793

VCD驱动RedHawk动态压降分析:替代vectorless的工程级电源完整性验证

动态IR drop是先进工艺节点下电源完整性(PI)验证的核心挑战,其本质是瞬态大电流在供电网络(PDN)中引发的电压坍塌现象。传统vectorless方法依赖翻转率与活动因子等统计均值,无法建模时序相关性、局部爆发性及相位差等关键动态特征,导致峰值电流低估37%以上,热点定位失真。VCD(Value Change Dump)文件以皮秒级时间精度记录真实功能向量下的信号翻转序列,为RedHawk提供高保真激励源,结合其Dynamic Power Grid Solver(DPGS)架构与自适应时间步长求解器,

weixin_33794672的博客 394

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

反向传值实例

反向传值实例 1.代理反向传值 #import <UIKit/UIKit.h> //声明一个类 @class LHTableViewController; //声明一个协议 @protocol LHTableViewControllerDelegate <NSObject> //协议中的方法 -(void)LHTablieViewContro...

weixin_30332241的博客 75

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

货拉拉2018秋招iOS笔试题解析:内存管理、Runtime与启动优化

iOS开发中,内存管理、运行时机制、多线程与性能优化是决定应用质量的关键能力。理解ARC下weak表如何清理弱引用、对象消息转发流程如何挽救崩溃,以及RunLoop如何驱动事件循环,是排查野指针、循环引用和卡顿问题的技术基石。从内存管理到启动流程,这些底层原理直接影响App的稳定性与用户体验,也是大型物流App保障司机端定位推送、用户端地图交互等高频场景的基础。本文以货拉拉2018秋招iOS笔试题为例,结合实际工程实践,拆解高频考点与出题逻辑,为校招复习提供参考。

weixin_34342992的博客 707

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Swift iOS开发实战:从ARC内存管理到App Store审核避坑指南

Swift作为iOS开发的核心语言,其价值远不止类型安全与语法简洁——它深度耦合UIKit生命周期、ARC内存模型与结构化并发机制。理解Optional如何根治空值崩溃、Result与async/await如何重构异步错误处理、@StateObject与@Binding如何划定状态边界,是构建高稳定性App的基础。尤其在真实生产环境中,内存泄漏、SIGABRT崩溃、网络超时、Preview预览失败等高频问题,本质都源于对Swift与iOS平台协同逻辑的误读。本文聚焦Swift在UIKit和SwiftUI双栈

ciyinzhi8788的博客 390

京东iOS校招笔试复盘:Runtime、内存管理、性能优化核心考点

在iOS开发中,Runtime是Objective-C动态特性的底层基础,它主导着消息发送、动态方法解析与消息转发等机制,深入理解Runtime原理能帮助开发者应对复杂业务场景。内存管理方面,从引用计数到weak自动置nil,再到autoreleasepool的边界,都是保障应用稳定性的关键。同时,多线程中的GCD死锁与信号量使用,以及UITableView的滑动流畅度优化,直接影响大型App的用户体验。这些技术点不仅是日常开发的基石,也是电商大厂校招笔试的高频考点。本文以京东2019校招iOS笔试题为例,

weixin_33895475的博客 401

ios cell 与tableview之间传参数block方式

    cell里面 typedef void(^ TouXiang)(NSString *); // 文章ID   @property (nonatomic, copy) TouXiang myTouXiang;    // 传文章ID的Block -(void)tapAction{     NSLog(@"点击了头像。");     self.myTouXiang(@"1")...

张明炜 440

iOS Block 处理UITableViewCell上button的点击事件

大家都知道在cell上添加button的话并没有什么难处, 直接添加就是,难处在于怎样处理button的点击事件.   那么怎样处理点击事件呢?在这里通过block的方式来处理cell上的button的点击事件. 前提工作:新建一个工程,  创建一个TableViewController,在cell上添加一个button 这里为了方便起见,使用storyboard建立了一个tableVi

KiuShuo的专栏 3304

XZ_iOS之block实现在cell和controller之间进行信息传递

使用block实现了在cell和controller之间进行值的传递。

understand_XZ的博客 1989

自定义UITableViewCell的两种方式

创建cell比较简单,但是有几个细节点我想说出来,和大家分享一下。使用纯代码创建,先是创建一个cell名字为customCell,继承于UITableViewCell.记着创建的时候不勾选使用XIB这一项,然后就是需要重写- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reu

学如逆水行舟 9292

Block很简单,就像delegate的简化版

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34194702的博客 179

TableView界面传值和TableViewCell使用

初始化-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNilP{ self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNilP]; if (self) { self.arr=[NSMutableArr

c_ios的博客 3271
下一篇: 动力效果 UIGravityBehavior- UICollisionBehavior- UIDynamicItemBehavior
TheForceCommander
博客等级 码龄11年 2粉丝 7原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值