2012-09-01 51 views
4

我想從我的UITableViewController與變化的IndexPath得到一個子類UITableViewCell(類稱爲'MasterCell')。比方說,我會在第一行獲取單元格:從UITableView與cellForRowAtIndexPath從UITableViewController獲取UITableViewCell

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0]; 
MasterCell *cell = (MasterCell *)[self.tableView cellForRowAtIndexPath:indexPath]; 

這發生在我的viewWillAppear方法中。不幸的是,調試時單元不存在。

我有不同的單元格/行,我必須在viewWillAppear方法中更改一些值。你有什麼提示嗎?

+0

你的意思是你試圖從故事板中獲得一個單元格嗎? – Darren

+0

是的,它是一個帶有TableViewController的故事板。 'self.tableView'是連接到TableView的屬性(IBOutlet UITableView)。 – Fonsi

+0

你不使用indexPath獲取單元格。在故事板中給它一個重用標識符,並使用MasterCell * cell =(MasterCell *)[tableView dequeueReusableCellWithIdentifier:@「masterCell」]; – Darren

回答

11

[self.tableView cellForRowAtIndexPath:indexPath]對於當前不可見的單元返回nil

您不應該使用單元格來存儲模型數據,因爲在向上或向下滾動時重複使用單元格。

+0

非常感謝你Darren和Martin R!我可以在方法' - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath')中設置初始值(它實際上是一個UIStepper,它從Core Data實體獲取它的值)。工作正常! – Fonsi

+0

@Fonsi:好的,歡迎來到stackoverflow。 –

+0

如何通過'self.tableView.subviews'訪問? – LKM

相關問題