您的位置:首页 > 产品设计 > UI/UE

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

2015-05-03 16:27 483 查看
这是我的第一篇关于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;

}

从而运行:



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

该项目的完整代码:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: