简体   繁体   English

为什么UITableView numberOfRowsInSection被多次调用?

[英]Why UITableView numberOfRowsInSection is called many times?

I am requesting data from CoreData . 我正在从CoreData请求数据。

There are two CoreData values. 有两个CoreData值。

I have this function to set the numbers of rows: 我有此功能来设置行数:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("TEST\n")
    return lebensmittel.count
}

The output of the print is: 打印输出为:

TEST

TEST

TEST

TEST

TEST

Why do I get the TEST 5 times when the lebentsmittel.count returns 2? 为什么当lebentsmittel.count返回2时,我获得5次测试?

The delegates methods may be called multiple times when your UITableView needs to update anything. 当您的UITableView需要更新任何内容时,可以多次调用委托方法。 By default, it's called very first time the UITableView is loaded or updated (reload data). 默认情况下,它是在第一次加载或更新UITableView (重新加载数据)时调用的。

It's for this that is not a good practice put anything more inside the numberOfRowsInSection . 为此,不是一个好习惯,在numberOfRowsInSection内部添加更多内容。

I hope this help you. 希望对您有所帮助。

use this code:-

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
            [ProductListStore removeAllObjects];
            return ProductListStore.count;
    }

remove array element.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM