表格numberofsectionsintableview numberOfRowsInSection被调用多次

I am making a REALLY BASIC UITableView project without any data source. I am just displaying “1” on first and only cell.

I am returning 1 in both numberOfSectionsInTableView and numberOfRowsInSection. I am also doing an NSLog on both methods simply stating the name of the respective methods.

On initial load, I get this:

SomeShit[27511:936083] number of sections
SomeShit[27511:936083] number of rows in section
SomeShit[27511:936083] number of sections
SomeShit[27511:936083] number of rows in section
SomeShit[27511:936083] number of sections
SomeShit[27511:936083] number of rows in section
Why are these methods calling thrice? Correct me if I am wrong but this is really weird. I thought they were supposed to call only once because of number of items in both being just 1.

EDIT:

What I am doing in viewDidLoad,

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
[self.view addSubview:self.tableView];

self.tableView.dataSource = self;
self.tableView.delegate = self;

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
2015年08月04日52分06秒

The delegate and data source are there to service the table view implementation, and methods may be called multiple times. You should not concern yourself with the implementation details of table views, just make sure your answers are consistent (i.e. you can answer correctly at any point in time and answers do not change without calling reloadData).

2015年08月04日52分06秒

Nowhere in the documentation does it say these methods are only call once in a reload data cycle.

You’ve made the assumption that they only need to be called once, but as you have shown, that’s a bad assumption.

After reviewing the documentation for UITableView and UITableViewDataSource, the only promise is this.

-[UITableView numberOfSections]:

Discussion
UITableView gets the value returned by this method from its data source and caches it.
This means you can’t know how calls to -[UITableView numberOfSections] will correspond with calls to -[UITableViewDataSource numberOfSectionsInTableView:].

Worse, the number of times these methods are called can (and have) change between different versions of iOS.

In short, make these methods idempotent and performant, and forget about implementation details of UITableView.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值