那些被面试过的iOS题目

1.创建timer有哪几种方式,有什么区别?CADisplayLink和NSTimer的区别?

2.UITableView那几个代理方法的调用顺序?假设有100个Cell,当前屏幕只能显示10个,调用reloadData后,heightForRow方法会执行多少次?预估高度这个属性是为了解决什么问题而诞生的,它有什么特性?

我测试了一下,测试设备:iphone6,系统8.1和10.0;

我们通过两种创建cell的方式来作为对比:
第一种:


#import "ViewController.h"

@interface ViewController ()
// 用来测试heightForRow方法的调用次数
@property (nonatomic, assign) NSInteger callTimes;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"reuseId"];
    self.callTimes = 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"%s", __func__);
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%s", __func__);
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s, 这是第%ld个组,第%ld个行的cell", __func__, indexPath.section, indexPath.row);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseId" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"这是第%ld个cell", indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.callTimes += 1;
    NSLog(@"%ld,这是第%ld个组,第%ld个行的cell", _callTimes, indexPath.section, indexPath.row);
    NSLog(@"%s", __func__);
    return ([UIScreen mainScreen].bounds.size.height - 64) / 2;
}

打印结果为:

2016-10-18 14:51:42.719 tableView测试[36378:3706092] -[ViewController numberOfSectionsInTableView:]
2016-10-18 14:51:42.719 tableView测试[36378:3706092] -[ViewController tableView:numberOfRowsInSection:]
2016-10-18 14:51:42.720 tableView测试[36378:3706092] 1,这是第0个组,第0个行的cell
2016-10-18 14:51:42.720 tableView测试[36378:3706092] -[ViewController tableView:heightForRowAtIndexPath:]
2016-10-18 14:51:42.720 tableView测试[36378:3706092] 2,这是第0个组,第1个行的cell
2016-10-18 14:51:42.720 tableView测试[36378:3706092] -[ViewController tableView:heightForRowAtIndexPath:]
2016-10-18 14:51:42.720 tableView测试[36378:3706092] 3,这是第0个组,第2个行的cell
2016-10-18 14:51:42.720 tableView测试[36378:3706092] -[ViewController tableView:heightForRowAtIndexPath:]
2016-10-18 14:51:42.721 tableView测试[36378:3706092] -[ViewController tableView:cellForRowAtIndexPath:], 这是第0个组,第0个行的cell
2016-10-18 14:51:42.721 tableView测试[36378:3706092] 4,这是第0个组,第0个行的cell
2016-10-18 14:51:42.721 tableView测试[36378:3706092] -[ViewController tableView:heightForRowAtIndexPath:]
2016-10-18 14:51:42.764 tableView测试[36378:3706092] 5,这是第0个组,第0个行的cell
2016-10-18 14:51:42.764 tableView测试[36378:3706092] -[ViewController tableView:heightForRowAtIndexPath:]
2016-10-18 14:51:42.765 tableView测试[36378:3706092] -[ViewController tableView:cellForRowAtIndexPath:], 这是第0个组,第1个行的cell
2016-10-18 14:51:42.765 tableView测试[36378:3706092] 6,这是第0个组,第1个行的cell
2016-10-18 14:51:42.765 tableView测试[36378:3706092] -[ViewController tableView:heightForRowAtIndexPath:]
2016-10-18 14:51:42.765 tableView测试[36378:3706092] 7,这是第0个组,第1个行的cell
2016-10-18 14:51:42.766 tableView测试[36378:3706092] -[ViewController tableView:heightForRowAtIndexPath:]

在以第二种方式创建cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s, 这是第%ld个组,第%ld个行的cell", __func__, indexPath.section, indexPath.row);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseId"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseId"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"这是第%ld个cell", indexPath.row];
    return cell;
}

这种方式打印结果为:

2016-10-18 14:45:27.626 tableView测试[36298:3680069] -[ViewController numberOfSectionsInTableView:]
2016-10-18 14:45:27.627 tableView测试[36298:3680069] -[ViewController tableView:numberOfRowsInSection:]
2016-10-18 14:45:27.627 tableView测试[36298:3680069] 1,这是第0个组,第0个行的cell
2016-10-18 14:45:27.628 tableView测试[36298:3680069] -[ViewController tableView:heightForRowAtIndexPath:]
2016-10-18 14:45:27.628 tableView测试[36298:3680069] 2,这是第0个组,第1个行的cell
2016-10-18 14:45:27.628 tableView测试[36298:3680069] -[ViewController tableView:heightForRowAtIndexPath:]
2016-10-18 14:45:27.628 tableView测试[36298:3680069] 3,这是第0个组,第2个行的cell
2016-10-18 14:45:27.629 tableView测试[36298:3680069] -[ViewController tableView:heightForRowAtIndexPath:]
2016-10-18 14:45:27.630 tableView测试[36298:3680069] -[ViewController tableView:cellForRowAtIndexPath:], 这是第0个组,第0个行的cell
2016-10-18 14:45:27.633 tableView测试[36298:3680069] 4,这是第0个组,第0个行的cell
2016-10-18 14:45:27.634 tableView测试[36298:3680069] -[ViewController tableView:heightForRowAtIndexPath:]
2016-10-18 14:45:27.635 tableView测试[36298:3680069] -[ViewController tableView:cellForRowAtIndexPath:], 这是第0个组,第1个行的cell
2016-10-18 14:45:27.635 tableView测试[36298:3680069] 5,这是第0个组,第1个行的cell
2016-10-18 14:45:27.635 tableView测试[36298:3680069] -[ViewController tableView:heightForRowAtIndexPath:]

对比之后,我们可以得出结论:
调用顺序:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

而且通过测试发现,通过第一种方式创建的cell在调用- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 方法时,最后会调用2次。

3.如何用TableView实现CollectionView,或者CollectionView实现tableView?

4.MVC设计模式解决了一个什么问题?

5.dispatch_barrier相关函数的作用?

6.一个View旋转后frame和bounds具体怎么变化的。

7.在A线程中发出的NSNotification在B线程中是否能收到?

8.什么时候子线程要开启NSRunloop,不开启的话会怎么样?(SDWebImage的原理)

http://blog.csdn.net/wzzvictory/article/details/9237973

9.tableView的性能优化有哪几种?什么是离屏渲染?

10.GCD和NSOperation的区别?

———————————— 以上是百度外卖的面试题 ——————————-

https和http的区别?

http://www.cnblogs.com/HedgehogBlog/p/4957561.html
http://www.jianshu.com/p/13c09f4e6fac

TCP/UDP的区别?

get和post的区别?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值