因为之前做iOS开发用的都是swift语言,没有用过oc,本篇博客整理一下UIKIt中组件基于oc的使用,顺便把每个组件在秋招中遇到的问题整理一下。
UIView的使用
UIView继承链
NSObject->UIResponder->UIView
UIView生命周期
loadView或nib文件加载view到内存 -> viewDidLoad函数初始化这些view -> 内存不足时, 调用viewDidUnload函数释放views -> 当需要使用view时回到第一步
UIView和CALayer的区别
1.功能不同,为了实现单一原则,降低耦合性,我们使用了UIView和CALayer,其中UIView主要是负责响应事件,而CALayer主要是为了进行视图的绘制和渲染
2.绘制方式不同,UIView是通过drawRect方法进行绘制的,而CALayer则是通过设置属性来进行绘制
3.渲染方式不同:UIView是通过Core Animation进行渲染的,而CALayer则是直接通过OpenGL进行渲染
4.动画支持不同:UIView可以通过UIView动画来实现动画效果,而CALayer可以通过Core Animation来实现动画效果
不过由于性能原因我们一般使用Layer进行绘制、渲染和动画
oc语言UIView的使用
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor redColor];
view.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:view];
//也可以在初始化的时候实现frame的建立——initWithFrame方法
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
//layer
CALayer * layer = [CALayer layer];
Layer.frame = CGRectMake(0, 0, 100, 100);
Layer.backgroundColor = xxx;
[self.View.layer addSublayer:layer];
UITableView的使用
UITableView继承链
NSObject->UIResponder->UIView->UIScrollView->UITableView/UIColltectionView
UITableView和UICollectionView的区别
-
数据展示方式不同:UITableView以单列的形式展示数据,每个单元格只能包含一个视图;UICollectionView以网格的形式展示数据,每个单元格可以包含多个视图。
-
布局方式不同:UITableView的布局方式是线性的,只能垂直滚动;UICollectionView的布局方式是网格的,可以垂直和水平滚动。
-
数据源协议不同:UITableView使用UITableViewDataSource和UITableViewDelegate两个协议来管理数据源和代理方法;UICollectionView使用UICollectionViewDataSource和UICollectionViewDelegate两个协议来管理数据源和代理方法。
-
复用机制不同:UITableView和UICollectionView都使用了复用机制来提高性能,但是UITableView只能重用单元格,而UICollectionView可以重用任何类型的视图。
-
功能不同:由于UICollectionView可以支持更灵活的布局方式和多种类型的单元格,因此在展示复杂的数据和实现自定义布局时更加方便。而UITableView则适用于简单的列表数据展示。
Tableview复用池机制
主要通过cell ID标识,cell ID相同的cell划出去会放到复用池中,等下一次滑到该cell ID对应的cell,使用复用池的cell
TableView的使用
转载自:iOS 构建合理的多类型cell的tableView,摆脱indexPath判断的嵌套 - 简书
1.数据
typedef NS_ENUM(NSInteger, DemoCellType) {
DemoTitleType,
DemoContentType,
DemoRecommendType,
DemoInputType,
DemoReportType
};
//如果是带section的界面,就构建二维数组
self.dataArray = @[@[@(DemoTitleType),@(DemoContentType)],@[@(DemoRecommendType),@(DemoInputType)],@[@(DemoReportType)]];
2.实现tableview代理事件<delegate,datasource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DemoCellType type = [self.dataArray[indexPath.section][indexPath.row] integerValue];
DemoBaseCell *cell;
switch (type) {
case DemoTitleType:
cell = [tableView dequeueReusableCellWithIdentifier:@"DemoTitleTypeCellId" forIndexPath:indexPath];
break;
case DemoContentType:{
cell = [tableView dequeueReusableCellWithIdentifier:@"DemoContentTypeCellId" forIndexPath:indexPath];
}
break;
default:
break;
}
cell.model = self.model;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DemoCellType type = [self.dataArray[indexPath.section][indexPath.row] integerValue];
DemoBaseCell *cell;
switch (type) {
case DemoTitleType:
break;
case DemoContentType:{
}
break;
default:
break;
}
}
//其他函数
UIButton
继承链
NSObject->UIResponder->UIView->UIControl->UIButton
UILabel
继承链
NSObject->UIResponder->UIView->UILabel