2012-09-18 66 views
1

我在一個UIView創建一個UITableView,只使用代碼只(不使用IB)iOS版 - UITableView的工作numberOfRowsInSection之間不正確的:和的cellForRowAtIndexPath:方法

m_tableData = [[UITableView alloc] init]; 
m_tableData.dataSource = self; 
m_tableData.delegate = self; 
[self addSubview:m_tableData]; 

而且

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSLog(@"%d", [arrPanelListImg count]); 
    return [arrPanelListImg count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kCell"]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"kCell"] ; 


     NSLog(@"%d", indexPath.row); 

    } 

return cell; 

}

而在日誌屏幕我看到

numberOfRowsInSection:方法返回18. 和cellForRowAtIndexPath:方法,只需運行9次(適合屏幕)。

而我在代碼中找不到任何奇怪的東西。所以在這種情況下有人對我有任何建議。 謝謝先進。

+4

這應該是正確的。它不會要求您填充數據,直到需要顯示它們。 –

回答

0

您需要使用UITableViewinitWithFrame:style:方法 - 這是UITableView類的指定初始值設定項。

0

你的意思是我原來的結果

m_tableData = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; 

當我改變它,沒有什麼不同。這真的很奇怪,這是我第一次看到這個錯誤,但仍然沒有解決方案。

編輯: - 只要刪除檢查條件單元格== nil並正常工作。

相關問題