我有一個表格,當我點擊一個單元格時,我想要訪問該單元格,以便可以更改該單元格的某些屬性,但由於某種原因,它創建了一個新的細胞每次。我可以這樣說,因爲- (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];
}
有沒有人有這是爲什麼每次都創建一個新的細胞,而不是給我的任何想法對被挖掘的單元格的引用?
我真的很感激任何幫助。
它可能與這行代碼有關:cell = [[AccountsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];你在哪裏創建一個新的單元格,如果tableview沒有退出。爲什麼你的表格視圖不能讓一個單元出隊? – Alex 2015-01-09 18:28:38
確保你註冊你的表視圖爲你的重用標識符,它可能每次調用cellForRowAtIndexPath總是創建一個新的單元格,因爲該標識符沒有註冊到tableview – 2015-01-09 18:36:34
要以編程方式註冊它,請使用'[tableView registerClass: [AccountsTableViewCell class] forCellReuseIdentifier:CellIdentifier];' – 2015-01-09 18:38:41