2011-09-03 87 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。