[非凡程序员]XibView tableViewXib

//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;

}

时间: 2024-08-08 08:42:22

[非凡程序员]XibView tableViewXib的相关文章

【非凡程序员】 OC第十五节课 (观察者模式和KVO进行对比)

今天主要学了观察者模式,以及回顾复习了KVO,两者进行对比 什么是观察者模式? 我们先打个比方,这就像你订报纸.比如你想知道美国最近放生了些新闻,你可能会订阅一份美国周刊,然后一旦美国有了新的故事,美国周刊就发一刊,并邮寄给你,当你收到这份报刊,然后你就能够了解美国最新的动态.其实这就是观察者模式,A对B的变化感兴趣,就注册为B的观察者,当B发生变化时通知A,告知B发生了变化.这是一种非常典型的观察者的用法,我把这种使用方法叫做经典观察者模式 KVO的全称是Key-Value Observer,

【非凡程序员】 OC第九节课 (KVO的应用)

这是一个QQ密码登陆地址和密码同时被修改时,通知用户QQ账号出现问题 该题难度主要在于判断监控的两次数据都是被修改的 有两种方案: ①定义一个可变的数组,把每次监控到的新旧数据添加进去,进行对比 ②定义一个变量,每修改一次,该变量就自加一次,判断为2时,通知用户 (1)main函数//  main.m//  QQ被盗////  Created by 非凡程序员 on 15/5/27.//  Copyright (c) 2015年 非凡程序员. All rights reserved.// #im

【非凡程序员】 OC第五节课 (数据类型NSString和NSString)

//  main.m//  ZiFuChuan////  Created by 非凡程序员 on 15/5/18.//  Copyright (c) 2015年 非凡程序员. All rights reserved.// #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {    @autoreleasepool { NSString *[email protected]"AB"

【非凡程序员】  OC第十节课 (代码块)

代码块就相当于C语言中一个函数一样 ////  main.m//  DaiMaKuai////  Created by 非凡程序员 on 15/5/28.//  Copyright (c) 2015年 非凡程序员. All rights reserved.// #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {    @autoreleasepool { int i=7; //代码块格式如下: 

[非凡程序员]手写UIDatePicker和UIPickerView

// //  ViewController.h //  手写UIDatePicker // //  Created by 非凡 程序员 on 15/11/13. //  Copyright (c) 2015年 非凡 程序员. All rights reserved. // #import <UIKit/UIKit.h> @interface  ViewController : UIViewController @property(nonatomic,strong)UIDatePicker *d

[非凡程序员]UIKit 手写控件转换大小写

// //  ViewController.m //  手写转换大小写 // //  Created by 非凡程序员 on 15/11/11. //  Copyright (c) 2015年 Querida. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [

【非凡程序员】 OC第十六节课 初识OS X开发二(设置时间和定闹钟练习)

设置时间和定闹钟练习: ViewController.h文件:#import <Cocoa/Cocoa.h> @interface ViewController : NSViewController- (IBAction)shezhijian:(id)sender;@property (weak) IBOutlet NSTextField *Song;@property (weak) IBOutlet NSTextField *ShiJian;@property (nonatomic,stro

【非凡程序员】 OC第十四节课 (代理模式 二 闹铃响了)

这是一个人定闹钟的例子,就是人委托闹钟叫醒自己 main函数: #import <Foundation/Foundation.h>#import "Person.h"#import "Clock.h" int main(int argc, const char * argv[]) {    @autoreleasepool  {        // insert code here...        NSLog(@"Hello, World!

【非凡程序员】 OC第九节课 (KVC的运算)

买粽子的例子,主要输练习KVC的应用,(注意:必须建立一个数组,才能使用KVC的运算)详细代码如下: 1.main函数代码: #import <Foundation/Foundation.h> #import "Person.h" #import "zongZi.h" int main(int argc, const char * argv[]) {     @autoreleasepool {         // insert code here..