分享

scrollView的重要属性,代理方法,tableView的数据源,代理方法,UIPickerView,UIDatePicker,UIToolbar

 叹落花 2015-05-18

scrollView 中的重要属性

  1. self.scrollView.contentSize=CGSizeMake(1784, 1264); // scrollView中的内容大小  
  2. self.scrollView.contentOffset=CGPointMake(500, 500);// scrollView中,图片原点与,屏幕原点的x,y轴  
  3. self.scrollView.contentInset=UIEdgeInsetsMake(10, 20, 40, 80);// scrollView 控件与内容的内边距  
  4.   
  5. self.scrollView.maximumZoomScale=210.f; //scrollView 中内容的最大放大尺寸  
  6. self.scrollView.minimumZoomScale=0.1f;  // scrollView 中内容的最小缩小尺寸  
  7.   
  8.   
  9. self.scrollView.delegate=self;  // 设置scrollView代理。 代理类必须实现UIScrollViewDelegate方法  


scrollView 中的代理方法

  1. // 开始拖拽时触发的代理事件  
  2. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{  
  3.     NSLog(@"将会开始拖拽");  
  4. }  
  5.   
  6. // 开始缩放时触发的代理事件  
  7. - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {  
  8.     NSLog(@"将会开始缩放");  
  9. }  
  10.   
  11. // 正在缩放时触发的代理事件  
  12. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{  
  13.     NSLog(@"正在缩放");  
  14.     return self.imageView;  
  15. }  

注:要实现缩放必须实现- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;方法,返回需要缩放的控件。



tableView添加数据源和代理方法

  1. self.tableView.dataSource=self; // 赋值类必须实现UITableViewDataSource协议  
  2. self.tableView.delegate=self;   // 赋值类必须实现UITableViewDelegate协议  



UITableView需要一个数据源(dataSource)来显示数据
UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等

凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源


tableView 数据源重要代理方法

  1. // 可以不实现。默认为1,返回数为,tableView的组数  
  2. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  3.     return 1;  
  4. }  
  5. // 返回值为每组的行数,参数,section为组数  
  6. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  7.     return self.heros.count;  
  8. }  
  9.   
  10. // 返回值为每行的TableViewCell indexPath中包含了。所在组与所在行  
  11. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  12.      
  13.     // 使用缓存池  
  14.     UITableViewCell *tableViewCell=[tableView dequeueReusableCellWithIdentifier:@"A"];  
  15.   
  16.     if(tableViewCell==nil){  
  17.         tableViewCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"A"];  
  18.     }  
  19.   
  20.     LFHero *hero=self.heros[indexPath.row];  
  21.     tableViewCell.textLabel.text=hero.name;  
  22.     tableViewCell.detailTextLabel.text=hero.intro;  
  23.     tableViewCell.imageView.image=[UIImage imageNamed:hero.icon];  
  24.       
  25.     tableViewCell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;  
  26.       
  27.     return tableViewCell;  
  28. }  

注:当滚动列表时,部分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];

例如:

  1. -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{  
  2.     if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {  
  3.         UILabel *timeLable=[[UILabel alloc]init];  
  4.         self.timeLable=timeLable;  
  5.         self.timeLable.textAlignment=NSTextAlignmentCenter;  
  6.         self.timeLable.font=[UIFont systemFontOfSize:10];  
  7.         self.timeLable.textColor=[UIColor grayColor];  
  8.         [self addSubview:timeLable];  
  9.           
  10.         UIButton *textButton=[UIButton buttonWithType:UIButtonTypeCustom];  
  11.         self.textButton=textButton;  
  12.         [self addSubview:textButton];        
  13.           
  14.         UIImageView *iconImageView=[[UIImageView alloc]init];  
  15.         self.iconImageView=iconImageView;  
  16.         [self addSubview:iconImageView];  
  17.     }  
  18.     return self;  
  19. }  


tableView 常用方法

  1. NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];  
  2. // 刷新指定行  
  3. [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];  
  4. // 刷新整个tableView  
  5. // 先更新表格,再滚动到指定行  
  6. [self.tableView reloadData];  
  7.   
  8. NSIndexPath *indexPath=[NSIndexPath indexPathForRow:self.messageFrames.count-1 inSection:0];  
  9.   
  10. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];  

UIPickerView


使用dataSource来获得行数和列数
dataSource必须实现UIPickerViewDataSource协议

  1. #pragma mark - UIPickerViewDataSource  
  2. // 返回pickerView一共有多少列  
  3. - (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView;  
  4.   
  5. // 返回pickerView的第component列有多少行  
  6. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;  


添加每一行每一列的内容必须使用delegate. 
delegate 需要实现UIPickerViewDelegate协议
  1. #pragma mark - UIPickerViewDelegate  
  2. // 返回第component列的第row行显示什么内容 单纯文字  
  3. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;  
  4.   
  5.   
  6. // 返回第component列的第row行需要显示的视图  
  7. // 当一个view进入视野范围内的时候调用  
  8. // 当系统调用该方法的时候会自动传入可重用的view  可以使用自定义视图  
  9. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view  
  10.   
  11.   
  12. // 当选中了pickerView的某一行的时候调用  
  13. // 会将选中的列号和行号作为参数传入  
  14. // 只有通过手指选中某一行的时候才会调用  
  15.   
  16. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;  

  1. // 返回第component列每一行的高度  
  2.   
  3. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;  

常用方法

  1.   // 刷新第1列对应的数据  
  2.         [pickerView reloadComponent:1];  
  3.         // 让第1列滚动到第0行  
  4.   
  5.         [pickerView selectRow:0 inComponent:1 animated:YES];  
  6.   
  7. // 让self.pickerView选中component 行,第row列  
  8.         [self.pickerView selectRow:row inComponent:component animated:YES];  
  9.   
  10. // 代码触发pickerView选中component 行,第row列 事件  
  11.         [self pickerView:self.pickerView didSelectRow:row inComponent:component];  


UIDatePicker 

创建时间选择器

  1. // 1.创建时间选择器  
  2.     UIDatePicker *datePicker = [[UIDatePicker alloc] init];  
  3.     // 设置只显示日期  
  4.     datePicker.datePickerMode = UIDatePickerModeDate;  
  5.     // 设置日期为中文  
  6.     datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];  
  7.   
  8.     datePicker.frame = CGRectMake(0, 44, 320, 162);  

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


UIToolbar 

.创建工具条  注:toolbar init的时候一定要设置frame不然会没法点击

  1. UIToolbar *toolbar = [[UIToolbar alloc] init];  
  2. toolbar.barTintColor = [UIColor purpleColor];  
  3. toolbar.frame = CGRectMake(0, 0, 320, 44);  
  4. [view addSubview:toolbar];  

给工具条添加按钮

  1. UIBarButtonItem *item0 = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:self action:@selector(previousBtnClick)];  
  2.     
  3.   UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];  
  4.     
  5.    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:self action:@selector(previousBtnClick)];  
  6.    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(previousBtnClick)];  
  7.   toolbar.items = @[item0, item1, item3, item2];  

先创建barItem,后添加监听事件

  1. UIBarButtonItem *barItem=[[UIBarButtonItem alloc]initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:nil action:nil];  
  2.  toolber.items =@[ barItem];  
  3.    
  4.  barItem.target=self;  
  5.   
  6.  barItem.action=@selector(test);  



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多