iOS UITableView、UICollectionView、UIWebView

UITableView

用来管理一组数据,继承自UIScrollView,可以滚动;可以分区(分组)显示内容,tabView中分区称为section,每一行称为row;frame属性决定tableView的显示位置,边框;而row表示tableView中的某一行的位置,row的数量决定contentSize的大小,tableView根据行数及行高自动计算它;每一行的位置都会放置一个UITableViewCell,负责显示本行的内容;有两种数据绑定方式,静态和动态绑定,其中静态单元格必须使用UITableViewController控制器;而动态绑定则没有限制约束;
1.常用属性

tableView.rowHeight = 60; // 统一设置行高,通常当每一行的内容样式相同并且高度固定时(高效);
tableview.sectionHeaderHeight = 44; // 设置每组的组标题的高度;
tableview.separatorColor // 分割线颜色
tableview.separatorStyle // 分割线样式
tableview.tableHeaderView // headerView
tableview.tableFooterView // 只能修改x和height的值,y和width不能修改;
tableView.tableFooterView.hidden = NO; // 控制是否显示FooterView
tableview.allowsSelection // 是否允许选中某一行;
tableView.sectionFooterHeight = 0;  // 组尾高度
tableView.sectionHeaderHeight = 10;  // 组头高度
tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);  
// 默认情况下UITableViewController中的tableView的第一行会有一段比较高的距离,可通过更改contentInset来改变此距离;

2、数据刷新

reloadData;
reloadInputViews;
reloadSectionIndexTitles;
reloadSections:(NSIndexSet *) withRowAnimation:(UITableViewRowAnimation);
[tableView reloadData];
// 重新刷新tableView,重新调用UITableView的数据源对象中的方法;
[tableView reloadRowsAtIndexPaths:
(NSArray *) withRowAnimation:(UITableViewRowAnimation)];
// 局部刷新,刷新指定的行,第一个参数array为需要刷新的行的数组,数组元素对象为NSIndexPath实例;

3.其它方法

[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPosition) animated:YES]; // 滚动到某一行
[tableView indexPathForSelectedRow];  // 获取当前选中的项
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
[tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationLeft]; // 刷新某组

4.样式设置
UITableViewController默认的tableView是plain样式,如果需要改成Grouped样式,则可以重写控制器的init方法

-(id)init { 
    return [self initWithStyle:UITableViewStyleGrouped]; 
}

tableView的backgroundView优先级要比backgroundColor要高,如果想要改变背景,则通常修改backgroundView=nil,再设置背景颜色;
其它:
<1>.设置section为多个section,再把UITableView样式改变Plain,则滚动的时候会把当前所滚动到的group的title显示到最顶端;
<2>.在iOS6下的UITableViewCell,默认contentView的frame的x为10,如果想要和iOS7一样,需要重写setFrame方法,让frame.size.width加10,frame.origin.x减10;

UITableViewDelegate

// 如果每行行高不一样,则需要通过代理的方式设置;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
// section组头的height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
// section组尾的height
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
// cell缩进级别
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; 
// section组头的view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
// section组尾的view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

注:不要在代理的获取组头/组尾View方法中直接返回一个UIView对象,因为这样无法实现重用该UIView,为了能重用每个Header中的UIView,这里需要返回一个UITableViewHeaderFooterView;

UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  // 每个section的row数量
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  //设置每个row上的cell

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
// section组头的title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
// section组尾的title
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
// section索引的title集合
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; 

UITableView数据源的添加,删除,移动,都需要对相应的数据源进行处理;
通常使用UITableViewController来替换UIViewController+UITableView的组合,前者是UIViewController的子类,内部封装了UITableView,实现了基本的代理方法;常用的一个属性是clearsSelectionOnViewWillAppear,默认值为NO;

UITableViewCell

列表数据显示基本单元格,显示每一行row的内容,系统默认提供四个样式:
UITableViewCellStyleDefault、Subtitle、Value1、Value2;

  • 编辑:editView、TableView被编辑时显示;
  • 内容:contentView,包含imageView,textLabel,detailTextLabel
  • 辅助:accessoryView,显示cell的辅助信息;

1.初始化

initWithStyle  // 只有在通过代码创建控制的时候才会调用,如果控件是通过xib或者storyboard创建的,则不会调用该方法;
awakeFromNib  // 如果控件是通过xib或者storyboard创建出来的,则调用此方法,此方法只会调用一次;
layoutSubviews:  // 当控件的frame被修改的时候就会被调用,一般在此方法中布局子控件;

注意:UITableViewCell在UITableView的代理方法cellAtIndex…方法中,不能设置它的cell元素的frame等属性,因为在return cell后,cell的layoutSubviews会自动覆盖之前设置的frame属性;
2.常用属性

imageView
textLabel
detailTextLabel

accessoryType // 辅助效果(系统提供默认三种样式,UITableViewCellAccessoryCheckmary,Disc...)
accessoryView

backgroundView
selectedBackgroundView
backgroundColor

selectionStyle // 选中效果
indentationLevel  缩进

3.Cell交互控制

// 辅助button被点击
-(void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
// cell将要取消选中
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
// cell将要被选中
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
// cell被选中
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// cell被取消选中
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
// 取消选中某行
- (void) deselectRowAtIndexPath: animated: 

优化:获取cell的方法中,把缓存tag设置为static,不会频繁创建该tag;通常单元格里面的view不是直接添加到UITableViewCell里面的,而是添加到UITableViewCell的contentView里面的;

4.自定义cell
<1>.新建一个类继承自UITableViewCell
<2>.在initWithStyle方法中对子控件进行初始化

  1. 将有可能显示的所有子控件都添加到contentView中
  2. 设置一些固定不变的属性

<3>.提供两个模型

  1. 数据模型
  2. frame模型(数据模型+所有子控件的frame+cell高度)

<4>.cell需要提供一个frame模型属性

  1. 将frame模型传递给cell
  2. cell根据frame模型给子控件设置frame,根据数据模型给子控件设置数据
  3. cell根据数据模型决定显示和隐藏哪些子控件

<5>.在tableView的代理方法中返回cell的高度

cell.backgroundView;  // 指定cell的背景view
cell.selectedBackgroundView;  // 指定cell的选中背景view

NSIndexPath

1.索引路径,用于标识row

+(NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;

2.属性

section
row

在刚刚创建好的headerView中获取的header的frame是0,当程序运行时,headerView就有frame了,因为在此方法中,将headerView返回后,UITableView在执行的时候,会用到headerView,UITableView既然要用headerView,则需要将headerView添加到UITableView中,当把headerView添加到UITableView的时候,UITableView内部会根据一些设置来动态的为headerView设置frame值,也就是说在UITableView即将用到headerView时,才会给它设置frame值;

3.UITableView的编辑

 1.tableView进入编辑状态 
 2.询问tableView的cell是否能够编辑
 3.询问tableView的cell的编辑类型
 4.tableView响应编辑操作

UITabelViewCell的移动

1.tableView进入编辑状态 
2.访问tableView能否移动 
3.确认tableView移动位置 
4.tableView响应移动操作

其它:
添加下拉刷新控件

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[tableView addSubView:refreshControl];
[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];

UICollectionView

用法基本和UITableView一样,也有数据源和代理;

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView // 返回section数量;
// 返回第section组有多少个item;
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
// 返回indexPath对应的section组的item项显示内容;
-(UICollectionViewCell *)collectionView: cellForItemAtIndexPath:(NSIndexPath *)indexPath

Scroll 函数

// 滚动到指定位置
[collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
// 获取当前可见的条目
NSIndexPath *path = [[collectionView indexPathsForVisibleItems] lastObject];
// scrollView停止滚动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

与UITableView的差别是:

1.通过dequeueXxx方法获取一个cell,如果缓存的cell为空,系统自动会为我们创建好cell并且返回回来,所以不用我们手动alloc init;
2.在获取cell之前,必须先注册一个cell到系统中(如果缓存池中没有identifier对应的cell,则会自动创建注册的class对应的cell);

[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];  // 通常把放到viewDidLoad方法中;

3.如果cell不是通过alloc init方法创建的,而是通过xib来创建的,则需要注册一个xib;

UINib *nib = [UINib nibWithNibName:@"cellItem" bundle:nil];
// 需要注意的是,要把cellItem.xib中的identifier设置为cell,和代码中相同;
[collectionView registerNib:nib forCellWithResuseIdentifier:@"cell"];

4.UICollectionView must be initialized with a non-nil layout parameter;
创建UICollectionView的时候,必须传入一个非空的layout参数;在自定义的UICollectionViewController中,重写init方法;

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];  // 创建一个UICollectionViewLayout,该布局对象决定将来CollectionView上每个item的显示方式;
layout.itemSize = CGSizeMake(100, 100);  // 设置每一个item的宽高;
layout.minmumLineSpacint = 20;  // 设置item行间距;minimumInteritemSpacint(列间距);
layout.headerReferenceSize = CGSizeMake(0, 100);  // 设置CollectionView距离上边的高度,(footerReferenceSize);
layout.sectionInset = UIEdgeInsetsMake(10, 20, 30, 40);  // 设置CollectionView的padding(上左下右);
self = [super initWithCollectionViewLayout:layout];  // 初始化的时候传入创建的布局对象;

UIWebView

-(void)loadRequest:(NSURLRequest *)request;
-(void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
// 加载本地文件
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];  // 根据路径获取URL;
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];  // 创建Request;
[webView loadRequest:request];

html跳转

window.location.href = "http://www.baidu.com";
window.loaction.href = "#elementId";
// 让webview执行javascript代码,跳转到指定位置;
[webview stringByEvaluatingJavaScriptFromString:@"window.location.href = '#xxId'"];
// document.getElementsByTagName('header')[0].remove(); 从Document中移除自己;
webview.scalesPageToFit = YES;  // 伸缩内容至适应屏幕;
[webview loadRequest:[NSURLRequest requestWithURL:"http://www.baidu.com"]];
// 如果webview在显示的时候没显示完全,可检查ViewController的Adjust Scroll View Insets属性;
-(void)reload;
-(void)stopLoading;
-(void)goBack|goForward;
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes;  // 需要进行检测的数据类型;
@property(nonatomic, getter=isLoading) BOOL loading;  // 是否正在加载中;
@property(nonatomic) BOOL scalesPageToFit;  // 伸缩内容到适应屏幕尺寸;

代理方法

-(BOOL)webView: shouldStartLoadWithRequest:request navigationType:navigationType
{
    NSString *url = request.URL.absoluteString;
    NSRange range = [url rangeOfString:@"ios://"];
    if (range.length !=0){
        NSString *method = [url substringFormIndex:range.location + range.length];
        SEL selector = NSSelectorFormString(method);  // 将方法转为SEL类型;
        [self performSelector:selctor withObject:nil];
    }
} // webview发送一个请求之前都会调用这个方法
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值