Uitableview 将行添加到tableview以添加行和核心数据

Uitableview 将行添加到tableview以添加行和核心数据,uitableview,core-data,Uitableview,Core Data,我创建了一个基于导航控制器的应用程序,它使用核心数据。在不首先修改启动应用程序的大部分代码的情况下,我希望能够通过在按下edit之后通过动态行添加行的选项来添加行 我发现的其他示例(如在上找到的示例)显示了所需的功能,但它不使用核心数据,因此我无法使用核心数据正确地翻译这一点 我已经看过了示例应用程序iPhoneCoreDataRecipes,该应用程序包含了所需的功能,但是示例非常复杂。基于示例应用程序,我将以下内容添加到-(UITableViewCell*)tableView:(UITabl

我创建了一个基于导航控制器的应用程序,它使用核心数据。在不首先修改启动应用程序的大部分代码的情况下,我希望能够通过在按下edit之后通过动态行添加行的选项来添加行

我发现的其他示例(如在上找到的示例)显示了所需的功能,但它不使用核心数据,因此我无法使用核心数据正确地翻译这一点

我已经看过了示例应用程序iPhoneCoreDataRecipes,该应用程序包含了所需的功能,但是示例非常复杂。基于示例应用程序,我将以下内容添加到-(UITableViewCell*)tableView:(UITableView*)tableView CellForRowatineXpath:(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;
}
//自定义表视图单元格的外观。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//对于“成分”部分,如有必要,请创建一个新单元格,并为其配置一个额外的数量标签。为该单元格指定一个与其他部分中的单元格不同的标识符,以便它可以单独出列。
id sectionInfo=[[self.fetchedResultsController sections]objectAtIndex:0];
NSInteger行=[sectionInfo numberOfObjects];
NSUInteger IngreditCount=行;
NSInteger行=indexPath.row;
if(indexPath.row
当我单击编辑按钮时,我可以删除行,但是我不会让添加的行单击以添加行。示例应用程序非常复杂,无法告诉我缺少什么。是否有要添加的功能,以自动将“添加行”按钮添加到表的末尾

编辑:添加了所有my.M文件以供参考@
当我在控制台中运行NSLog的1-12显示时。我当前没有尝试添加“添加行”行到核心数据,因为每次用户按下导航栏中的编辑按钮时,都会添加或删除该行。

是否已更改数据源的
numberOfRowsInSection
方法,以说明所需的额外行?

您需要做的是将新对象添加到数据库中,然后,假设tableView的数据源是数据库中的数组,则在tableView上调用reloadData。

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