scrollView 中的重要属性
- self.scrollView.contentSize=CGSizeMake(1784, 1264); // scrollView中的内容大小
- self.scrollView.contentOffset=CGPointMake(500, 500);// scrollView中,图片原点与,屏幕原点的x,y轴
- self.scrollView.contentInset=UIEdgeInsetsMake(10, 20, 40, 80);// scrollView 控件与内容的内边距
-
- self.scrollView.maximumZoomScale=210.f; //scrollView 中内容的最大放大尺寸
- self.scrollView.minimumZoomScale=0.1f; // scrollView 中内容的最小缩小尺寸
-
-
- self.scrollView.delegate=self; // 设置scrollView代理。 代理类必须实现UIScrollViewDelegate方法
scrollView 中的代理方法
- // 开始拖拽时触发的代理事件
- - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
- NSLog(@"将会开始拖拽");
- }
-
- // 开始缩放时触发的代理事件
- - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
- NSLog(@"将会开始缩放");
- }
-
- // 正在缩放时触发的代理事件
- - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
- NSLog(@"正在缩放");
- return self.imageView;
- }
注:要实现缩放必须实现- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;方法,返回需要缩放的控件。
tableView添加数据源和代理方法
- self.tableView.dataSource=self; // 赋值类必须实现UITableViewDataSource协议
- self.tableView.delegate=self; // 赋值类必须实现UITableViewDelegate协议
UITableView需要一个数据源(dataSource)来显示数据
UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等
凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源
tableView 数据源重要代理方法
- // 可以不实现。默认为1,返回数为,tableView的组数
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return 1;
- }
- // 返回值为每组的行数,参数,section为组数
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return self.heros.count;
- }
-
- // 返回值为每行的TableViewCell indexPath中包含了。所在组与所在行
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- // 使用缓存池
- UITableViewCell *tableViewCell=[tableView dequeueReusableCellWithIdentifier:@"A"];
-
- if(tableViewCell==nil){
- tableViewCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"A"];
- }
-
- LFHero *hero=self.heros[indexPath.row];
- tableViewCell.textLabel.text=hero.name;
- tableViewCell.detailTextLabel.text=hero.intro;
- tableViewCell.imageView.image=[UIImage imageNamed:hero.icon];
-
- tableViewCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
-
- return tableViewCell;
- }
注:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给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模型数据只加载一次)
注:initWithStyle:reuseIdentifier中创建UIButton一定要使用[UIButton buttonWithType:UIButtonTypeCustom];
例如:
- -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
- if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
- UILabel *timeLable=[[UILabel alloc]init];
- self.timeLable=timeLable;
- self.timeLable.textAlignment=NSTextAlignmentCenter;
- self.timeLable.font=[UIFont systemFontOfSize:10];
- self.timeLable.textColor=[UIColor grayColor];
- [self addSubview:timeLable];
-
- UIButton *textButton=[UIButton buttonWithType:UIButtonTypeCustom];
- self.textButton=textButton;
- [self addSubview:textButton];
-
- UIImageView *iconImageView=[[UIImageView alloc]init];
- self.iconImageView=iconImageView;
- [self addSubview:iconImageView];
- }
- return self;
- }
tableView 常用方法
- NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
- // 刷新指定行
- [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
- // 刷新整个tableView
- // 先更新表格,再滚动到指定行
- [self.tableView reloadData];
-
- NSIndexPath *indexPath=[NSIndexPath indexPathForRow:self.messageFrames.count-1 inSection:0];
-
- [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
UIPickerView
使用dataSource来获得行数和列数
dataSource必须实现UIPickerViewDataSource协议
- #pragma mark - UIPickerViewDataSource
- // 返回pickerView一共有多少列
- - (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView;
-
- // 返回pickerView的第component列有多少行
- - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
添加每一行每一列的内容必须使用delegate.
delegate 需要实现UIPickerViewDelegate协议
- #pragma mark - UIPickerViewDelegate
- // 返回第component列的第row行显示什么内容 单纯文字
- - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
-
-
- // 返回第component列的第row行需要显示的视图
- // 当一个view进入视野范围内的时候调用
- // 当系统调用该方法的时候会自动传入可重用的view 可以使用自定义视图
- - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
-
-
- // 当选中了pickerView的某一行的时候调用
- // 会将选中的列号和行号作为参数传入
- // 只有通过手指选中某一行的时候才会调用
-
- - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
- // 返回第component列每一行的高度
-
- - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
常用方法
- // 刷新第1列对应的数据
- [pickerView reloadComponent:1];
- // 让第1列滚动到第0行
-
- [pickerView selectRow:0 inComponent:1 animated:YES];
-
- // 让self.pickerView选中component 行,第row列
- [self.pickerView selectRow:row inComponent:component animated:YES];
-
- // 代码触发pickerView选中component 行,第row列 事件
- [self pickerView:self.pickerView didSelectRow:row inComponent:component];
UIDatePicker
创建时间选择器
- // 1.创建时间选择器
- UIDatePicker *datePicker = [[UIDatePicker alloc] init];
- // 设置只显示日期
- datePicker.datePickerMode = UIDatePickerModeDate;
- // 设置日期为中文
- datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
-
- datePicker.frame = CGRectMake(0, 44, 320, 162);
2.监听UIDatePicker的选择
* 因为UIDatePicker继承自UIControl,所以通过addTarget:...监听
UIToolbar
.创建工具条 注:toolbar init的时候一定要设置frame不然会没法点击
- UIToolbar *toolbar = [[UIToolbar alloc] init];
- toolbar.barTintColor = [UIColor purpleColor];
- toolbar.frame = CGRectMake(0, 0, 320, 44);
- [view addSubview:toolbar];
给工具条添加按钮
- UIBarButtonItem *item0 = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:self action:@selector(previousBtnClick)];
-
- UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
-
- UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:self action:@selector(previousBtnClick)];
- UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(previousBtnClick)];
- toolbar.items = @[item0, item1, item3, item2];
先创建barItem,后添加监听事件
- UIBarButtonItem *barItem=[[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:nil action:nil];
- toolber.items =@[ barItem];
-
- barItem.target=self;
-
- barItem.action=@selector(test);
|