2011-03-18 142 views
0

這是cellForRowAtIndexPath:方法的代碼。爲什麼[self.tableView reloadData]不起作用?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; 

if (cell == nil) { cell = [[[UITableViewCell alloc] 
    initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease]; 
} 

NSUInteger row = [indexPath row]; 
cell.textLabel.text = [array objectAtIndex:row]; 
return cell;  
} 

當我使用[self.tableView reloadData];什麼都沒有發生?

+1

很少Q ?:這個類是UITableViewController的子類嗎?在調用[self.tableView reloadData]之前使用新值更新數組; – Hanuman 2011-03-18 07:32:06

+0

不,是我做的.. – mamrezo 2011-03-18 07:39:23

回答

10

您需要至少將tableView:numberOfRowsInSection:方法作爲UITableViewDataSource協議的一部分和tableView:cellForRowAtIndexPath:(您已經完成)。您還應該執行numberOfSectionsInTableView:

然後,你需要確保你的類實際上是用作數據源:

self.tableView.dataSource = self; 

需要通過界面生成器來:做或在awakeFromNib或類似viewDidLoad:一些其他的方法。

1

你檢查你的dataSource和UITableView的委託嗎?我認爲dataSource是零或別的東西。

+0

它顯示的值 – mamrezo 2011-03-18 07:37:40

+0

嗯,我假設你爲UITableView實現dataSource和委託,你確定你的[self.tableView reloadData]被調用了嗎?我認爲視圖出現時會顯示某些內容。而且,像Hanuman說的那樣,檢查你的本地數據,以便在reloadData之前更新dataSource。 – AechoLiu 2011-03-18 08:37:11

+0

我做了所有,當我添加對象viewDidload它顯示,但是當我嘗試重新加載它不工作 – mamrezo 2011-03-18 09:48:48

1

嘗試在行的單元格中放置一個斷點並查看是否在您調用reloadData時調用它。同時請檢查是否已經對單元格填充的數組進行了更改,並確保表視圖連接到其數據源和委託。