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

本文介绍了一个iOS新手在使用UITableView时遇到的数据顺序传递和反向传递的问题及解决方案。通过创建项目,设置UITableView,定义数据源,实现协议和委托方法,详细解释了如何在两个UIViewController之间进行数据的传递和接收,包括从ViewController到MyPicturesViewController的正向传递,以及从MyPicturesViewController回传到ViewController的反向传递。最后,文章提到了如何根据选择的行显示对应图片的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是我的第一篇关于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中,并指定其segueidentifier

 *

 *  @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时,

/**

 *  将在MyPicturesViewControllercell的对应的图片的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;

}

从而运行:



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

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


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值