2011-09-03 77 views
0

我已經創建了一個使用核心數據的基於導航控制器的應用程序。首先,我不想修改起始應用程序中的大部分代碼,我希望能夠通過在按下編輯後通過動態行添加行的選項來添加行。將行添加到tableview以添加行和核心數據

其他的例子,我發現,如一個在this site發現顯示所需的功能但是它不使用核心數據,所以我一直沒能正確地翻譯這個使用的核心數據。

我已經看過示例應用程序iPhoneCoreDataRecipes,該應用程序包含所需的功能,但該示例非常複雜。基於示例應用程序,我已經添加了以下到我的 - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath功能

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
// For the Ingredients section, if necessary create a new cell and configure it with an additional label for the amount. Give the cell a different identifier from that used for cells in other sections so that it can be dequeued separately. 
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0]; 

NSInteger rows = [sectionInfo numberOfObjects]; 
NSUInteger ingredientCount = rows; 
NSInteger row = indexPath.row; 

if (indexPath.row < ingredientCount) { 
    // If the row is within the range of the number of ingredients for the current recipe, then configure the cell to show the ingredient name and amount. 
    static NSString *IngredientsCellIdentifier = @"IngredientsCell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:IngredientsCellIdentifier]; 

    if (cell == nil) { 
     // Create a cell to display an ingredient. 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IngredientsCellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    // 
    [self configureCell:cell atIndexPath:indexPath]; 
} else { 
    // If the row is outside the range, it's the row that was added to allow insertion (see tableView:numberOfRowsInSection:) so give it an appropriate label. 
    NSLog(@"---- IN ADD INGREDIENTS SECTION ----"); 
    static NSString *AddIngredientCellIdentifier = @"AddIngredientCell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:AddIngredientCellIdentifier]; 
    if (cell == nil) { 
     // Create a cell to display "Add Ingredient". 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AddIngredientCellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 
    cell.textLabel.text = @"Add Ingredient"; 
} 
return cell; 
} 

當我點擊編輯按鈕,我可以刪除行,但是我沒有得到添加的行來點擊添加行。示例應用程序非常複雜,可以告訴我錯過了什麼。是否有一個函數添加到自動添加'添加行'按鈕的表末尾?

編輯:添加的所有參考我的.m文件的@http://pastebin.com/Ld7kVts7 當我跑我的NSLog的1-12顯示在控制檯中。我目前並未嘗試將「添加行」行添加到核心數據,因爲每次用戶在導航欄中按下編輯按鈕時都會添加或刪除該行。

回答

0

您是否已將數據源的numberOfRowsInSection方法更改爲您所需的額外行?

+0

是的。 section方法中的行數包含以下內容:id sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; NSInteger rows = [sectionInfo numberOfObjects]; if(self.editing){+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ } 返回行;' – propstm

0

你需要做的是將新的對象添加到你的數據庫,然後,假設你的tableView的dataSource是一個數組的形式,請在tableView上調用reloadData。