您的位置:首页 > 职场人生

[非凡程序员]XibView tableViewXib

2015-11-17 00:00 465 查看
摘要: 非凡程序员,感谢 黄杨超ios指导,总结:毕绒超学习笔记

//Xibs就是布局界面的一种方式。

//创建xib的方法:选择New->File->UserInterface->Empty 设置将要创建Xib的文件名字,

//============ 第一种: 首先,关于Xib加载 这里以“XibLoadView”为例==========//

//--------------进入XibLoadView.xib文件-----------

//创建好Xib文件后,我们看到Xib中没有任何页面,拖进一个View控件,此时View控件不能调节大小,设置View的size是Freeform,拖入应有的控件。

//--------------进入Viewcontroller.m文件------------
//1.加载Xib文件。
/*
bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.
我们的程序是一个bundle. 在Finder中,一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的main bundle

通过使用下面的方法得到程序的main bundle
NSBundle *myBundle = [NSBundle mainBundle];//一般我们通过这种方法来得到bundle.
一旦我们有了NSBundle 对象,那么就可以访问其中的资源了
*/

//加载方法:[ [ NSBundle mainBundle ] loadNibNamed:@"Xib文件名" owner:nil options:nil ] ;

//============第二种 其次,关于Xib控件属性 这里以“XibViewOne”为例==========//
//与第一种方法的不同,这样的Xib可以获得控件的对象,设置每一个控件的属性

//--------------进入XibLoadView.xib文件-----------

//创建好Xib文件后,我们看到Xib中没有任何页面,拖进一个View控件,此时View控件不能调节大小,设置View1的size是Freeform后在看,这是View就可以改变大小了。调整好View的大小。设置backgroundColor的颜色以便确认是否加载成功。拖Label控件标示说明当前View

//--------------进入Viewcontroller.h文件------------
//拖线,将控件拖进viewController的接口文件里,注意要将file's owner的class变为ViewController.

//--------------进入Viewcontroller.m文件------------
//加载Xib。注意owner变量为self

//============第三种 关于Xib控件 这里以“XibViewTwo”为例,创建Class名为ViewTwo==========//
//这种是将Xib文件和Class分开创建,建好后在进行变量关联的
//--------------进入XibLoadView.xib文件-----------

//创建好Xib文件后,拖入需要控件,将控件大小设置好,设置文件的File's Owner 对应的Class。设置栏填写ViewTwo

//--------------进入ViewTwo.h文件------------
//拖线。注意:如果创建的为NSObject类需要引入UIKit框架

//--------------进入Viewcontroller.m文件------------
//1.引入头文件,ViewTwo.h 2.实例化ViewTwo的对象
//2.加载Xib。注意owner变量为ViewTwo的对象
//3.获取View

#import "ViewController.h"
#import "ViewTwo.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//============ 关于Xib加载 ==========//

//加载Xib。当Xib在经过编译链接之后就变为nib文件。nib在bundle目录下,先获取程序的mainbundle,在其中加载指定的nib文件,这里owner为nil是由于,没有owner变量

NSArray * array = [ [ NSBundle mainBundle ] loadNibNamed:@"XibLoadView" owner:nil options:nil ] ;

UIView * ViewLoadFromXib = [ array firstObject ] ;//在取出View时注意,这里我们可以用[ array lastObject ],由于这个Xib只有一个View

[ self.view addSubview : ViewLoadFromXib ] ;//添加到根视图

//============ 其次,关于Xib控件属性 这里以“XibViewOne”为例==========//

NSArray * arrayOne = [ [ NSBundle mainBundle ] loadNibNamed:@"XibViewOne" owner:self options:nil ] ;//这里的owner是

_viewOne = [ arrayOne firstObject ] ;//在取出View时注意,这里我们可以用[ array lastObject ],由于这个Xib只有一个View
_viewOne.frame=CGRectMake(0, 100, 375, 100);

[ self.view addSubview : _viewOne ] ;//添加到根视图

//============ 关于Xib控件 这里以“XibViewTwo”为例==========//
//实例化ViewTwo对象
ViewTwo * viewOnwer = [ [ ViewTwo alloc ] init ] ;
NSArray * arrayViewTwo = [[ NSBundle mainBundle ] loadNibNamed:@"XibViewTwo" owner:viewOnwer options:nil];
UIView * view = [ arrayViewTwo firstObject ] ;
view.frame=CGRectMake(0, 210, 375, 100);
viewOnwer.label.text=@"XibViewTwo-___-";
[ self.view addSubview:view ] ;

}

@property (weak, nonatomic) IBOutlet UIView *viewOne;
@property (weak, nonatomic) IBOutlet UILabel *lable;

//ViewTwo.h

@property (weak, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) IBOutlet UIView *View;

//XIB
- (void)viewDidLoad {
[super viewDidLoad];
ViewOnwer *onwer=[[ViewOnwer alloc]init];

UIScrollView * scrollView=[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
for (int i=0; i<6; i++) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"View" owner:onwer options:nil];
UIView * view =[array firstObject];

onwer.label.text=@"移动迷宫2";
onwer.image.image=[UIImage imageNamed:@"2"];
[onwer.button setTitle:@"下载" forState:UIControlStateNormal];
[onwer.button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[onwer.button addTarget:self action:@selector(Down:) forControlEvents:UIControlEventTouchUpInside];
view.frame = CGRectMake( 0, i * view.frame.size.height, view.frame.size.width, view.frame.size.height);
[scrollView addSubview:view];

}
scrollView.contentSize=CGSizeMake(0,onwer.viewCell.frame.size.height * 6 );
[self.view addSubview:scrollView];

}
-(void)Down:(UIButton *)sender{
[sender setTitle:@"正在下载" forState:UIControlStateNormal];
}

@interface ViewOnwer : NSObject
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *button;
@property (strong, nonatomic) IBOutlet UIView *viewCell;

//在列表里样式有很多种,我们要注意一般的TableView是没有图片的,当我们需要某种样式的TableViewCell。我们可以借助继承UITableViewCell类的来创建自己的tableView
//1.New File -> ios Souce -> Cocoa Touch Class (UITableViewCell为父类)注意勾选同时创建Xib选项
//2.在创建的类里重写实例化方法(-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reusename)-----去TableViewCell.m里看~
//3.有cell就要相应的容器装,创建UITableView。回到ViewCintroller的ViewDidLoad方法。
//4.遵守协议(两个UITableViewDelegate和UITableViewDataSource),设置实现者
//5.实现必要方法
//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//6.绘制cell的时候注意这是的cell的样式是我们设置好的,将除去原本方法返回外,所有UITableViewCell的类转化为自定义类
//7.调整行距。-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
tableView.delegate=self;
tableView.dataSource=self;
[self.view addSubview:tableView];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 4;
}
//绘制cell的方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
cell=[[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.cellLabel.text=@"hello";
cell.cellImage.image=[UIImage imageNamed:@"0"];

return cell;
}
//7.调整行距。这是设置行高的方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}

@implementation TableViewCell
//这是同时创建的,不用修改Class!!!
//拖拽注意控件的Subject为TableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reusename{
self=[super initWithStyle:style reuseIdentifier:reusename];//创建的子类的对象要实现父类的方法

NSArray * array = [[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil];//加载Xib
self=[array firstObject];//返回的View为Xib设置好的

return self;//返回自己设置好的行
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: