iOS笔记3

来源:互联网 发布:天浩网络打印服务器 编辑:程序博客网 时间:2024/05/17 19:15


UITableView创建不同高度的cell
1.新建一个继承自UITableViewCell的类


2.重写initWithStyle:reuseIdentifier:方法
添加所有需要显示的子控件(不需要设置子控件的数据和frame,  子控件要添加到contentView中)
进行子控件一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片)


3.提供2个模型
数据模型: 存放文字数据\图片数据
frame模型: 存放数据模型\所有子控件的frame\cell的高度


4.cell拥有一个frame模型(不要直接拥有数据模型)


5.重写frame模型属性的setter方法: 在这个方法中设置子控件的显示数据和frame


6.frame模型数据的初始化已经采取懒加载的方式(每一个cell对应的frame模型数据只加载一次)


代理:
先搞清楚谁是谁的代理(delegate)


定义代理协议,协议名称的命名规范:控件类名 + Delegate


定义代理方法
代理方法一般都定义为@optional
代理方法名都以控件名开头
代理方法至少有1个参数,将控件本身传递出去


设置代理(delegate)对象  (比如myView.delegate = xxxx;)
代理对象遵守协议
代理对象实现协议里面该实现的方法


在恰当的时刻调用代理对象(delegate)的代理方法,通知代理发生了什么事情
(在调用之前判断代理是否实现了该代理方法)


键盘相关操作:
通过UITextField的代理方法能够监听键盘最右下角按钮的点击
成为UITextField的代理
self.textField.delegate = self;


遵守UITextFieldDelegate协议,实现代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField;




在UITextField左边放一个view
self.textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 0)];
self.textField.leftViewMode = UITextFieldViewModeAlways;


键盘相关的通知:
UIKeyboardWillShowNotification // 键盘即将显示
UIKeyboardDidShowNotification // 键盘显示完毕
UIKeyboardWillHideNotification // 键盘即将隐藏
UIKeyboardDidHideNotification // 键盘隐藏完毕
UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变
UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕


系统发出键盘通知时,会附带一下跟键盘有关的额外信息(字典),字典常见的key如下:
UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frame
UIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后)
UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间
UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)


代理和通知的选择:
共同点
利用通知和代理都能完成对象之间的通信
(比如A对象告诉D对象发生了什么事情, A对象传递数据给D对象)


不同点
代理 : 一对一关系(1个对象只能告诉另1个对象发生了什么事情)
通知 : 多对多关系(1个对象能告诉N个对象发生了什么事情, 1个对象能得知N个对象发生了什么事情)






 某个控件出不来,控件没显示:
 1.frame的尺寸和位置对不对
 
 2.hidden是否为YES
 
 3.有没有添加到父控件中
 
 4.alpha 是否 < 0.01
 
 5.被其他控件挡住了
 
 6.父控件的前面5个情况
 */


 
 1.Xcode插件的安装路径(可以在这里删除内容来卸载插件)
(aplle是用户名)
/Users/aplle/Library/Application Support/Developer/Shared/Xcode/Plug-ins


2.UIView自带的方法
1> - (void)layoutSubviews;
* 当一个控件的frame发生改变的时候就会自动调用
* 一般在这里布局内部的子控件(设置子控件的frame)
* 一定要调用super的layoutSubviews方法


2> - (void)didMoveToSuperview;
* 当一个控件被添加到父控件中就会调用


3> - (void)willMoveToSuperview:(UIView *)newSuperview;
* 当一个控件即将被添加到父控件中会调用


 pch文件的作用:
 1.存放一些全局的宏(整个项目中都用得上的宏)
 2.用来包含一些全部的头文件(整个项目中都用得上的头文件)
 3.能自动打开或者关闭日志输出功能
 #ifdef DEBUG  // 调试阶段
#define RTLog(...) NSLog(__VA_ARGS__)
#else // 发布阶段
#define RTLog(...)

#endif


一.UIPickerView
1.UIPickerView的常见属性
// 数据源(用来告诉UIPickerView有多少列多少行)
@property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;
// 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择)
@property(nonatomic,assign) id<UIPickerViewDelegate>   delegate;
// 是否要显示选中的指示器
@property(nonatomic)        BOOL                       showsSelectionIndicator;
// 一共有多少列
@property(nonatomic,readonly) NSInteger numberOfComponents;


2.UIPickerView的常见方法
// 重新刷新所有列
- (void)reloadAllComponents;
// 重新刷新第component列
- (void)reloadComponent:(NSInteger)component;


// 主动选中第component列的第row行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;


// 获得第component列的当前选中的行号
- (NSInteger)selectedRowInComponent:(NSInteger)component;


3.数据源方法(UIPickerViewDataSource)
//  一共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
//  第component列一共有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;


4.代理方法(UIPickerViewDelegate)
//  第component列的宽度是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
//  第component列的行高是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;


//  第component列第row行显示什么文字
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;


//  第component列第row行显示怎样的view(内容)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;


//  选中了pickerView的第component列第row行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;


二.UIDatePicker
1.常见属性
// datePicker的显示模式
@property (nonatomic) UIDatePickerMode datePickerMode;
// 显示的区域语言
@property (nonatomic, retain) NSLocale   *locale;


2.监听UIDatePicker的选择
* 因为UIDatePicker继承自UIControl,所以通过addTarget:...监听


三.程序启动的完整过程
1.main函数


2.UIApplicationMain
* 创建UIApplication对象
* 创建UIApplication的delegate对象


3.delegate对象开始处理(监听)系统事件(没有storyboard)
* 程序启动完毕的时候, 就会调用代理的application:didFinishLaunchingWithOptions:方法
* 在application:didFinishLaunchingWithOptions:中创建UIWindow
* 创建和设置UIWindow的rootViewController
* 显示窗口


3.根据Info.plist获得最主要storyboard的文件名,加载最主要的storyboard(有storyboard)
* 创建UIWindow
* 创建和设置UIWindow的rootViewController
* 显示窗口

0 0