2015-08-28 128 views
1

我正在請求CoreData的數據。爲什麼UITableView numberOfRowsInSection被多次調用?

有兩個CoreData值。

我有這個函數來設置行數:

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

打印的輸出是:

TEST 

TEST 

TEST 

TEST 

TEST 

爲什麼我得到測試5次,當lebentsmittel.count返回2?

+2

因爲它被稱爲各種各樣的時間,只要需要它。如果你重新加載數據,如果你滾動並需要顯示另一個「隱藏」行,它將計算是否有行顯示等。 – Larme

回答

0
use this code:- 

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

remove array element. 
2

當您的UITableView需要更新任何內容時,可能會多次調用代表方法。默認情況下,在第一次加載或更新UITableView時重新調用(重新加載數據)。

這是不是一個好的做法,把更多的東西放在numberOfRowsInSection裏面。

我希望這對你有所幫助。

相關問題