博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发25-iOS可视化编程-Interface Builder
阅读量:3531 次
发布时间:2019-05-20

本文共 3603 字,大约阅读时间需要 12 分钟。

iOS开发25-iOS可视化编程-Interface Builder

 
 有问题请联系博主,邮箱:nathanlee1987@aliyun.com

Interface Builder简称IB,是Mac OX X 和iOS平台下用于设计和测试用户界面的应用程序。

提供了拖放面板,可以将控件拖放到屏幕上。IB创建.xib文件,包括视图布局,以XML格式存储。
程序运行后,.xib文件中的内容编译为.nib文件(二进制文件),存储在工程包中。
nibNameOrNil是.xib文件的名字
nibNameOrNil如果填nil,默认找与类同名的.xib文件(比如LoginViewController.xib LoginView.xib)可以直接使用init方法,默认找相关的.xib文件

1、创建RootViewController和RootViewController.xib文件

注意选中 Also create XIB file,就会创建一个同名的XIB文件

AppDelegate.m中初始化ViewController

#import "AppDelegate.h"#import "RootViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    self.window.backgroundColor = [UIColor whiteColor];        [self.window makeKeyAndVisible];            //第1种      //  RootViewController * rootVC=[[RootViewController alloc]initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];       //第2种    //    RootViewController *rootVC=[[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];        //第3种    //RootViewController *rootVC=[[RootViewController alloc]initWithNibName:nil bundle:nil];           //第4种    RootViewController *rootVC=[[RootViewController alloc]init];        self.window.rootViewController=rootVC;                  return YES;}

如果创建ViewController的时候没有选择一同创建XIB文件的话

可以自己创建一个XIB文件关联上ViewController

 

2、向XIB中添加UITableView

遵守代理,并实现方法,注册UITbableViewCell

#import "RootViewController.h"@interface RootViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableview;@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 10;}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; cell.textLabel.text=@"测试数据"; return cell;}@end

运行效果:

UITableView并没有占满屏幕,需要设置一下约束

效果:

3、自定义UITableViewCell

设置约束:

添加到UITableView中

- (void)viewDidLoad {    [super viewDidLoad];    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];            //代码创建cell注册方式--这里不能用     //[self.tableview registerClass:[MyCell class] forCellReuseIdentifier:@"mycell"];        //xib注册方式    [self.tableview registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:@"mycell"];}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{    return 2;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 5;}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 100;}-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    //    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];//    cell.textLabel.text=@"测试数据";        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell" forIndexPath:indexPath];        cell.myLabel.text =@"123测试";        return cell;}
效果:

 
 有问题请联系博主,邮箱:nathanlee1987@aliyun.com
著作权声明:本文由http://my.csdn.net/Nathan1987_原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢
你可能感兴趣的文章
[ 笔 记 ] 主动信息收集_002
查看>>
[ CTF ] ssh私钥泄漏_笔记
查看>>
设计模式学习
查看>>
操作系统学习总结
查看>>
Java JSON字符串与自定义类/基本类型相互转换
查看>>
Java中时间戳和时间格式的转换
查看>>
Dubbo基础知识整理
查看>>
计算机网络知识整理
查看>>
Java基础知识
查看>>
操作系统知识整理
查看>>
实现自己的权限管理系统(二):环境配置以及遇到的坑
查看>>
实现自己的权限管理系统(四): 异常处理
查看>>
实现自己的权限管理系统(十):角色模块
查看>>
实现自己的权限管理系统(十二):权限操作记录
查看>>
实现自己的权限管理系统(十三):redis做缓存
查看>>
实现自己的权限管理系统(十四):工具类
查看>>
JavaWeb面经(一):2019.9.14
查看>>
JavaWeb面经(二):2019.9.16 Synchronized关键字底层原理及作用
查看>>
JavaWeb面试经:redis
查看>>
牛客的AI模拟面试(1)
查看>>