UITableView numberOfSectionsInTableView和numberOfRowsInSection在初始加载时调用三次 [英] UITableView numberOfSectionsInTableView and numberOfRowsInSection called thrice on initial load

查看:1665
本文介绍了UITableView numberOfSectionsInTableView和numberOfRowsInSection在初始加载时调用三次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个没有任何数据源的真正的基础UITableView 项目。我只是在第一个和唯一的单元格上显示1。

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

numberOfSectionsInTableView 和<返回1 code> numberOfRowsInSection 。我也在两个方法上做了一个 NSLog ,只是说明了各个方法的名称。

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

为什么这些方法调用三次?如果我错了,请纠正我,但这真的很奇怪。我认为他们应该只打一次电话,因为两者中的物品数量只有1个。

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.

编辑:

我在做什么 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];


推荐答案

代表和数据源用于服务表视图实现,并且可以多次调用方法。您不应该关注表视图的实现细节,只需确保您的答案是一致的(即您可以在任何时间点正确回答,如果不调用 reloadData )。

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).

这篇关于UITableView numberOfSectionsInTableView和numberOfRowsInSection在初始加载时调用三次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆