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

iOS --- 在UITableView中使用segue进行页面跳转的注意事项 在之前的一篇学习记录UINavigationController的简单总结中, 介绍了使用UINavigationController及segue实现View之间的跳转, 并在两个View之间进行数据的传递. 如果该segue是连接UITableViewCell与WebViewController, 点击单个cell的时候, 发现页面会跳转至WebViewController, 但想要的数据却并未成功通过segue. 阅读详情

这是我的第一篇关于IOS的文章,可能有些不对的地方,希望大家指正。

步骤:
一:
1.建立一个项目,其ViewController的类型是UITableViewController,设置其identifier——MyTextCell;(点击viewcontroller的控制器后 点击上面的标题栏Editor中的embed in中的Navigation)
2.添加一个新的controller,MyPicturesViewController继承UITableViewController,设置其identifier——MyPicCell;
3.建立segue,从ViewController的控制器按住ctrl添加segue到MyPicturesViewController,选择show,其segue的identifier是ShowPic
初步的view视图工作完成了。



第二部分就是代码的,
二:
1.为ViewController添加数据源,创建newGroup——ModelClass,在ModelClass中创建cocoa Touch class的NSObject类ViewControllerInfo。
2.在ViewControllerInfo中初始化一些信息


ViewControllerInfo.h文件中:

#import<Foundation/Foundation.h>

@interface ViewControllerInfo :NSObject

@property (nonatomic,strong)NSArray *cellInfo;

@end


ViewControllerInfo.m文件中:

#import "ViewControllerInfo.h"

@implementation ViewControllerInfo

- (id)init{

    if (self = [superinit]) {

        [selfinitCellInfo];

    }

    return self;

}

- (void)initCellInfo{

    _cellInfo = [[NSArrayalloc]initWithObjects:@"内容一",@"内容二",@"内容三",nil];

}

@end

ViewController.m文件中:


3.给ViewController中的Cell初始化内容。

#import "ViewController.h"

#import "ViewControllerInfo.h"

#import "MyPicturesViewController.h"


@interfaceViewController ()


@property (nonatomic,strong)ViewControllerInfo *viewInfo;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    [selfconfigureViewInfo];

}

//加载数据。

- (void)configureViewInfo{

    _viewInfo = [[ViewControllerInfoalloc]init];

}

//行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    returnself.viewInfo.cellInfo.count;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

   return1;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   staticNSString *viewControllerCellIdentifier =@"MyTextCell";

   UITableViewCell *viewCell = [tableViewdequeueReusableCellWithIdentifier:viewControllerCellIdentifier];

   if (viewCell ==nil) {

        viewCell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:viewControllerCellIdentifier];

    }

    viewCell.textLabel.text =self.viewInfo.cellInfo[indexPath.row];

    

   return viewCell;

}


这样在viewController中cell就有值了。

4.将cell中的内容作为MyPicturesViewController的题目(数据的顺序传值)。

/**

 *  该方法是将点击的cell中的值传入到sender中,并指定其segue的identifier

 *

 *  @param tableView <#tableView description#>

 *  @param indexPath <#indexPath description#>

 */

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        

   NSString *needTransInfo =self.viewInfo.cellInfo[indexPath.row];

    [selfperformSegueWithIdentifier:@"ShowPic"sender:needTransInfo];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

   if ([segue.identifierisEqualToString:@"ShowPic"] && [segue.destinationViewControllerisKindOfClass:[MyPicturesViewControllerclass]]) {

       MyPicturesViewController *myPicViewController = segue.destinationViewController;


//将sender中的值传到myPicViewController对象中myTitle变量中。


        myPicViewController.myTitle = sender;

    }

}


(需要在MyPicViewController.h中定义NSString的没有 myTitle)

@property (nonatomic,strong)NSString *myTitle;



5.初始化MyPicViewController中信息,在modelClass中添加一个类PicInfo.先添加一些数据。(当然在.h文件中有一个NSArray的变量cellInfo)

PicInfo.m文件中:

#import "PicInfo.h"


@implementation PicInfo

- (id) init{

   if (self = [superinit]) {

        [selfinitPicCellInfo];

    }

    returnself;

}

- (void) initPicCellInfo{

    _cellInfo = [[NSArrayalloc]initWithObjects:@"image1.jpg",@"image2.jpg",@"image3.jpg",nil];

}


@end


需要导入三张图片:


6.初始化MyPicViewController

MyPicViewController.m文件中:


#import "MyPicturesViewController.h"

#import "PicInfo.h"


@interfaceMyPicturesViewController ()

@property (nonatomic,strong)PicInfo *picInfo;


@end

@implementation MyPicturesViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    [selfconfigureData];

    

    [selfsetTitle:self.myTitle];

}

- (void)configureData{

    _picInfo = [[PicInfoalloc]init];

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


    // Return the number of sections.

   return1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    // Return the number of rows in the section.

    returnself.picInfo.cellInfo.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

   staticNSString *cellInfoIdentifier =@"MyPicCell";

   UITableViewCell *viewCell = [tableViewdequeueReusableCellWithIdentifier:cellInfoIdentifier];

   if (viewCell ==nil) {

        viewCell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellInfoIdentifier];

    }

   UIImage *cellImage = [UIImageimageNamed:self.picInfo.cellInfo[indexPath.row]];

    viewCell.imageView.image = cellImage;

   return viewCell;

}


这样MyPicViewController中的cell就有图片了
7.将MyPicViewController中的图片传到ViewController中的cell中(反向传递数据)将会用到协议delegate,在MyPicViewController.h定义协议,实现协议中的方法。

#import <UIKit/UIKit.h>

@classMyPicturesViewController;

@protocol transmitPicDelegate <NSObject>

//定义协议需要实现的方法,也就是需要在一个controller中将图片传到另一个controller中

- (void)choicePic:(MyPicturesViewController *)controller didChoicePic:(NSString *)PicName;


@end


@interface MyPicturesViewController :UITableViewController

@property (nonatomic,strong)NSString *myTitle;


@property (nonatomic,strong)id <transmitPicDelegate> delegate;


@end



接着在MyPicViewController.m文件中,当点击一行cell时,

/**

 *  将在MyPicturesViewController中cell的对应的图片的name获得到

 *

 *  @param tableView <#tableView description#>

 *  @param indexPath <#indexPath description#>

 */

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

   NSString *imageName =self.picInfo.cellInfo[indexPath.row];

   UITableViewCell *newCell = [tableViewcellForRowAtIndexPath:indexPath];

    

    if(newCell.accessoryType ==UITableViewCellAccessoryNone)

    {

        

        newCell.accessoryType =UITableViewCellAccessoryCheckmark;

        

        newCell.textLabel.textColor = [UIColorblueColor];

        

    } else{

        newCell.accessoryType =UITableViewCellAccessoryNone;

    }

//    NSLog(@"%@",imageName);

//调用协议中的方法传入实参

    [self.delegatechoicePic:selfdidChoicePic:imageName];

}



8.在ViewController.h导入MyPicViewController.h之后,遵守该协议

#import <UIKit/UIKit.h>

#import "MyPicturesViewController.h"


@interface ViewController :UITableViewController<transmitPicDelegate>


@end


在ViewController.m中具体实现协议中的方法。

- (void)choicePic:(MyPicturesViewController *)controller didChoicePic:(NSString *)PicName{

   self.imageName = PicName;

//    NSLog(@"%@",self.imageName);

    [self.tableViewreloadData];

    

}


当然在这个文件中需要定义imageName的变量,而reloadData是将这个视图的数据重新加载一遍。

@interfaceViewController ()


@property (nonatomic,strong)ViewControllerInfo *viewInfo;

@property (nonatomic,strong)NSString *imageName;


@end



那么需要把myPicViewController设置成自己的代理。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

   if ([segue.identifierisEqualToString:@"ShowPic"] && [segue.destinationViewControllerisKindOfClass:[MyPicturesViewControllerclass]]) {

       MyPicturesViewController *myPicViewController = segue.destinationViewController;

        

       myPicViewController.delegate = self;

        

        

        myPicViewController.myTitle = sender;

    }

}

同时需要在方法中将获取的图片name设置成此controller的cell中的UIImageView.image;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *viewControllerCellIdentifier =@"MyTextCell";

    UITableViewCell *viewCell = [tableView dequeueReusableCellWithIdentifier:viewControllerCellIdentifier];

    if (viewCell == nil) {

        viewCell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:viewControllerCellIdentifier];

    }

    viewCell.textLabel.text =self.viewInfo.cellInfo[indexPath.row];

    

    viewCell.imageView.image = [UIImage imageNamed:self.imageName];

    

    return viewCell;

}

运行之后,点击内容一

       





若想只是把点击所对应的那行添加图片,这样的话应该怎样弄呢?

只需在ViewController中定义一个NSInteger的变量indexNumber纪录当点击一行的行数,


@property (nonatomic)NSInteger indexNumber;

然后在,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    

    self.indexNumber = indexPath.row;

    NSString *needTransInfo = self.viewInfo.cellInfo[indexPath.row];

    [selfperformSegueWithIdentifier:@"ShowPic"sender:needTransInfo];

}

再在,(红色的部分)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *viewControllerCellIdentifier =@"MyTextCell";

    UITableViewCell *viewCell = [tableViewdequeueReusableCellWithIdentifier:viewControllerCellIdentifier];

    if (viewCell == nil) {

        viewCell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:viewControllerCellIdentifier];

    }

    viewCell.textLabel.text =self.viewInfo.cellInfo[indexPath.row];

    if (self.indexNumber == indexPath.row) {

        viewCell.imageView.image = [UIImage imageNamed:self.imageName];

    }

    

    

    return viewCell;

}

从而运行:



实现了数据的顺序和反向传递。

该项目的完整代码:点击打开链接


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

相关推荐

ios-分段切换控制器(倒置的tableView).zip

用tableView实现滑动界面,既简单又不会卡顿。

微信数据恢复终极指南:当你的聊天记录消失时该怎么办?

在数字时代,微信聊天记录承载着我们的工作沟通、珍贵回忆和重要信息。但当系统崩溃、手机丢失或账号异常时,这些加密的数据似乎永远消失了。今天,我将为你揭秘一个曾经存在的工具——PyWxDump,以及它在合规框架下的技术原理,帮助你理解微信数据恢复的完整逻辑。 ## 🔍 微信数据加密的真相:为什么你的聊天记录无法直接查看? 微信作为全球最流行的即时通讯工具,其数据安全机制设计得相当完善。所有聊天记

gitblog_00716的博客 1130

工地卸料平台扬尘安全管控建设方案(含设备选型清单).docx

工地卸料平台扬尘安全管控建设方案(含设备选型清单)

iOS 开发-数组反向显示于 tableView。

效果类似 iOS 上的系统秒表功能下的 tableView 显示方式。 代码如下 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *Identifier = @"Cell"

command.Zi的专栏 2003

硅谷刷屏的ClawdBot,让Mac mini卖爆了!创始人爆料:一人开发、100% AI 写代码

这两天,个人 AI 助手 ClawdBot 席卷硅谷,国内外社交平台上全是关于它的讨论。 在试用后,网友们纷纷给出了很高的评价。“它是迄今为止最伟大的 AI 应用,相当于你 24 小时全天候专属 AI 员工。”Creator Buddy 创始人兼 CEO Alex Finn 盛赞道,“这就是他们 (Anthropic) 希望 Claude Cowork 呈现的样子。”

技术引领业务创新 1598

《LangChain技术原理剖析:OpenAI Function Calling的实践与应用》

,"unit": {},name:函数的唯一标识符:自然语言描述,决定LLM如何选择该函数parameters:定义输入参数的完整Schema。

qq_62223405的博客 987

一种让UITableView的数据从下往上增长的方式

遇到问题 一般来说tableview的数据都是从上往下增长,如下图所示(先是aaa出现在表格列表的最顶部,然后bbb出现在aaa的下面,以此类推) 但是如果我们想反向这个过程该怎么做呢?如下图所示(先是aaa出现在表格列表的最底部,然后aaa被顶上去,bbb出现在aaa原来的位置) 解决问题 我们当然第一时间向万能的stackoverflow君求救,找到了这个解决方案。 原理很简单就是让整个t...

weixin_30355437的博客 331

c++11 无锁队列

/************************************************************************* &gt; File Name: lockfreequeue.cpp &gt; Author: &gt; Mail: &gt; Created Time: Wed 08 Aug 2018 11:38:08 AM CST ********...

qq_35728402的博客 5033

Objective-C中列表倒序

<br /><br />@interface NSArray (Reverse)<br /> <br />- (NSArray *)reversedArray;<br /> <br />@end<br /> <br /> <br />@interface NSMutableArray (Reverse)<br /> <br />- (void)reverse;<br /> <br />@end<br /> <br /> <br /><br />@implementation NSArray (Reverse

周辉的专栏 1657

【Burp入门第三十七篇】AutoRepeater插件安装使用教程+越权实战案例

AutoRepeater 通过接收满足特定条件的请求,按照预设的基础替换规则对请求进行处理。在接收到匹配请求后,它首先应用基础替换,然后针对每个定义的替换创建请求副本并应用该替换操作,从而实现对不同参数的修改和请求的重复发送,以此来全面检测 Web 应用的授权情况。1、自动化请求重复:能自动复制、修改并重发 HTTP 请求,避免了手动重复操作的繁琐,大大提高了 Web 应用授权测试的效率。2、灵活参数替换:可灵活改变邮箱地址、账号身份、角色、URL 和 CSRF 令牌等关键参数。

等风来 2041

9.23华为OD机试真题 新系统 - 世界杯徽章换购 (Java/Py/C/C++/Js/Go)

9.23华为OD机试真题 新系统 - 世界杯徽章换购 (Java/Py/C/C++/Js/Go)

算法大师! 1821

让UITableView自动滑动(定位)到某一行cell

NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:10 inSection:0]; [[self tableView] scrollToRowAtIndexPath:scrollIndexPath         atScrollPosition:UITableViewScrollPositionTop animat

lovenjoe的专栏 1万+

AUTOSAR进阶图解==>AUTOSAR_SWS_DefaultErrorTracer

AUTOSAR Default Error Tracer (DET)是AUTOSAR架构中的核心错误追踪模块,提供统一的错误报告机制。DET能够处理开发错误、运行时错误和瞬态故障三类问题,支持模块级和实例级的配置管理。模块架构上,DET通过Det_API接口与其他模块交互,可配置转发错误至诊断事件管理器(DEM)或调试日志追踪(DLT)。其配置结构包含全局参数和模块实例参数,允许灵活控制错误报告行为。DET在AUTOSAR 4.3.0版本中从DevelopmentErrorTracer扩展而来,增强了错误处

一名留德华的,康德的信徒的,专注于汽车电子领域的博客 919

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

我们都习惯说 反向传值用Block,但是正向和反向都只是相对的 下面我 先 用一个自定义cell来给button“正向”传值,效果如下图(带颜色的是重点哦) 1、“正向”传值 首先创建 UITableView #pragma mark*******创建UITableView*******-(void)createT

TheForceCommander的博客 2636

C#上位机+TensorFlow Lite 轻量化AI模型部署实战(工业级完整版,低算力工控机首选)

工业自动化轻量化AI部署指南:C# + TensorFlow Lite实战 摘要:本文针对工业自动化领域机器视觉部署的痛点,提出基于C#和TensorFlow Lite(TFLite)的轻量化解决方案。该方案完美适配低算力工控环境(J1900/2G内存),具有以下核心优势:1)极致轻量化,模型体积仅KB-MB级;2)CPU深度优化,单帧推理≤15ms;3)零环境依赖,纯C#部署;4)工业级稳定性,7×24小时运行。教程涵盖从环境搭建、模型转换到C#封装的完整流程,特别提供YOLOv8等工业常用模型的TFLi

这里只有干货:从 Modbus 到 MES,从 YOLO 到工控机部署;65 篇现场排障案例,每篇都写到改哪一行。 1040

贝叶斯(Bayes)决策理论

在博文【模式识别PR和模式的概念】介绍了相关的模式识别与模式的概念。在本节描述所要讨论的问题之前,再提一下对于待识别的物理对象的描述问题。假设一个待识别的物理对象用其d个属性观察值描述,称之为d个特征,这组成一个d维的特征向量,而这d维待征所有可能的取值范围则组成了一个d维的特征空间。一般地,机器实现自动分类有两大类方法:一种是模板匹配方法,另一种是对特征空间划分为子空间(每类的势力范围)的方法。

松子茶的专栏 3万+

即时通讯UI-聊天界面(UITableView显示左右两人聊天)

目录 1.创建UITableView对象并设置相关属性 2.创建cellModel模型 //枚举类型 typedef enum { ChatMessageFrom = 0,//来自对方的消息 ChatMessageTo //发给对方的消息 }ChatMesageType; #import <Foundation/Founda...

weixin_33810006的博客 327

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

IOS-数组的倒序、升序、降序

NSMutableArray *array = [NSMutableArray arrayWithObjects:@"5",@"3",@"4",@"2",nil];    // 倒序NSMutableArray *resultArr = (NSMutableArray *)[[array reverseObjectEnumerator] allObjects];NSLog(@"倒序:%@",res...

吴德文的博客 3523

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

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

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

IOS菜鸟的所感所思(三)——通过服务器接口获取数据

通过服务器接口获取数据,ios

MyCodeDancer的博客 5781

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Xcode9学习笔记50 - 调整UITableView单元格的顺序

import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {//表格视图数据源协议、表格视图代理协议 var months = ["January", "February", "March", "April", "May", "June", "July",

LisenH的博客 523

Swift之UITableView的增删以及三种逆向传值方式

对于swift一直提不起来兴趣,老是不想学。。。前2周公司部门内需要做内部技术分享。这两周以来断断续续的看着,感觉真的是看起来真是没啥大的作用,除非真正的去用。才能发现一些问题。所以,大胆的去看文档吧,边看边实践,你会有不一样的收获的哦。 下面的这个例子呢,很简单,主要就是Swift下的UITableView的数据的展示以及数据增加删除,再就是通知,闭包,代理这三种传值方式,涉及到的知识点还是很多

Lyanla的专栏 1508

iOS tableView动态创建分组及组内排序

#pragma mark - 判断分组数,并且按上传时间和检查时间降序排列 -(void)analyseHDCData{          //1.数组内部元素排序     NSArray *sortDesc = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"createTime" a

Dancer2015 的博客 3888

ios tableview是否滚到顶部和底部以及滚动的方向

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (self.tab.contentOffset.y <= 0) { //顶部 } else if (self.tab.contentSize.height - self.tab.contentOffset.y-self.tab.fr

梦若在心就在的博客 4467

iOS开发系列--UITableView全面解析

--UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信、QQ、新浪微博等软件基本上随处都是UITableView。当然它的广泛使用自然离不开它强大的功能,今天这篇文章将针对UITableView重点展开讨论。今天的主要内容包括: 基本介绍 数据源 代理 性能优化 UITableVi...

weixin_33749242的博客 515

IOS UITableView索引排序功能

UITbableView分组展示信息时,有时在右侧会带索引,右侧的索引一般为分组的首字母,比如城市列表的展示。当点击右侧索引的字母,列表会快速跳到索引对应的分组,方便我们快速查找。下面,就介绍一下索引的最简单地设置。 设置表格索引的步骤: 1、添加表格,设置代理和数据源 2、得到要显示的数据的数组 3、得到右侧显示索引的数组,索引数组中元素的个数要

tiebanshao的博客 692
下一篇: IOS菜鸟的所感所思(二)——UIKit中的UIDynamicAnimator
FingerCode
博客等级 码龄12年 7粉丝 16原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值