2014-10-12 79 views
1

在我的項目中,我通過代碼創建了一切,如tableviews和cells。我有添加單元格的功能。 情況是:我有10個對象在我的數組中,當程序運行時,函數(- (TodoTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath)在我滾動時貫穿所有10個單元,它在單元類中運行10次init ...函數。但是,當我在程序中添加一個新單元格時,我發現這個新單元格由於某種原因從未調用init函數。 (正成功創建的細胞,雖然...奇怪)IOS-TableView Cell:dequeueReusableCellWithIdentifier issue

我用這部分來補充細胞:

[self.tableView beginUpdates]; 
    [self.todoItemList insertObject:newItem atIndex:toBeAddedIndex]; 
    [self.tableView insertSections: [NSIndexSet indexSetWithIndex:toBeAddedIndex] withRowAnimation:UITableViewRowAnimationNone]; 
    [self.tableView endUpdates]; 

,並在這個函數:

- (TodoTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
NSLog(@"Scroll to %ld", indexPath.section); 
static NSString *cellIdentifier = @"TodoCell"; 

TodoTableViewCell *cell = (TodoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 

//If the current cell is dummycell, make it transparent 
cell.todoItem = (TodoItem *)(self.todoItemList[indexPath.section]); 

我不知道爲什麼,請人好心幫

---------- -----------------更新

NSString *cellIdentifier = [NSString stringWithFormat:@"TodoCell%ld",indexPath.section]; 

我發現如果我改成這一行。每次添加一個新的單元格時,它都會調用init函數!但是...如果我先刪除然後再添加,問題仍然存在,因爲這個標識符已經被創建和使用並且我想是存儲的。

回答

1

嘗試撥打:

[self.tableView insertRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>] 
+0

問題是,我使用的部分,而不是行,因爲我需要的細胞之間的差距。所以函數:heightForHeaderInSection對我來說非常有用。 – Colafroth 2014-10-12 15:33:36

+0

我試過了:[self.tableView insertSections:[NSIndexSet indexSetWithIndex:toBeAddedIndex] withRowAnimation:UITableViewRowAnimationNone]; \t \t [self.tableView insertRowsAtIndexPaths:@ [[NSIndexPath indexPathForRow:0 inSection:toBeAddedIndex]] withRowAnimation:UITableViewRowAnimationNone];新創建的單元的init函數仍然沒有被調用。 – Colafroth 2014-10-12 15:41:47

+0

你打給哪個init?我想你應該調用 - (instancetype)initWithStyle:(UITableViewCellStyle)風格reuseIdentifier :(NSString *)reuseIdentifier – 2014-10-12 16:04:08