2015-01-09 76 views
0

我有一個表格,當我點擊一個單元格時,我想要訪問該單元格,以便可以更改該單元格的某些屬性,但由於某種原因,它創建了一個新的細胞每次。我可以這樣說,因爲- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier每次點擊一個單元格時都會被調用。點擊一個UITableView單元格創建一個新的單元格

這裏是我創建的單元代碼:

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

    static NSString *CellIdentifier = @"AccountsCell"; 

    NSLog(@"AccountsTableViewController : cellForRow"); 

    AccountsTableViewCell *cell = (AccountsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (nil == cell) 
    { 

     cell = [[AccountsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 

    [cell setCurrentAccount:[self.accounts objectAtIndex:indexPath.row]]; 

    cell.delegate = self; 

    return cell; 

} 

和選擇單元格:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    AccountsTableViewCell *cell = (AccountsTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath]; 

} 

有沒有人有這是爲什麼每次都創建一個新的細胞,而不是給我的任何想法對被挖掘的單元格的引用?

我真的很感激任何幫助。

+0

它可能與這行代碼有關:cell = [[AccountsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];你在哪裏創建一個新的單元格,如果tableview沒有退出。爲什麼你的表格視圖不能讓一個單元出隊? – Alex 2015-01-09 18:28:38

+0

確保你註冊你的表視圖爲你的重用標識符,它可能每次調用cellForRowAtIndexPath總是創建一個新的單元格,因爲該標識符沒有註冊到tableview – 2015-01-09 18:36:34

+0

要以編程方式註冊它,請使用'[tableView registerClass: [AccountsTableViewCell class] forCellReuseIdentifier:CellIdentifier];' – 2015-01-09 18:38:41

回答

5

你基本上通過重新重新運行tableView:cellForRowAtIndexPath方法與tableViewindexPath作爲參數創建一個新的AccountsTableViewCell

而是寫的:

AccountsTableViewCell *cell = (AccountsTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath]; 

嘗試

AccountsTableViewCell *cell = (AccountsTableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; 

indexPath得到tableView的當前單元格。

+0

對此我無法表示感謝,這已經讓我發瘋了,你可能只是救了我的工作。 – SeanT 2015-01-09 20:56:00

+0

@SeanT哈哈沒問題。很高興你不會失去你的工作。 ;) – 2015-01-09 20:56:45

相關問題