1

我的应用程序编译得很好,但是当我点击头文件中的“新建”按钮时,它崩溃了,返回了这个崩溃消息:

2012-04-30 11:44:40.599 Homepwner[13233:f803] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1021
2012-04-30 11:44:40.601 Homepwner[13233:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted).'
*** First throw call stack:
(0x13cd022 0x155ecd6 0x1375a48 0x9ae2cb 0x9c103 0xa76d2 0xa7711 0x39ff 0x13cee99 0x1a14e 0x1a0e6 0xc0ade 0xc0fa7 0xc0266 0x3f3c0 0x3f5e6 0x25dc4 0x19634 0x12b7ef5 0x13a1195 0x1305ff2 0x13048da 0x1303d84 0x1303c9b 0x12b67d8 0x12b688a 0x17626 0x294d 0x28b5)
terminate called throwing an exception(lldb) 

一直在查看我的 ItemsViewController.m 但无法找出问题所在,因为我没有更改任何 UITableView 方法。

你能告诉我有什么问题吗?提前致谢。

项目视图控制器.m

#import "ItemsViewController.h"
#import "BNRItemStore.h"
#import "BNRItem.h"

@implementation ItemsViewController // Incomplete implementation

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  DetailViewController *detailViewController = [[DetailViewController alloc]init];

  NSArray *items = [[BNRItemStore sharedStore]allItems];
  BNRItem *selectedItem = [items objectAtIndex:[indexPath row]];

  //Give detail view controller a pointer to the item object in a row
  [detailViewController setItem:selectedItem];

  // Push it onto the top of the navigation controller's stack
  [[self navigationController]pushViewController:detailViewController animated:YES];
}

-(id)init
{
  // Call the superclass's designated initializer
  self = [super initWithStyle:UITableViewStyleGrouped];
  if (self) {
    UINavigationItem *n = [self navigationItem];

    [n setTitle:@"Homepwner"];

  }
  return self;
}

-(id)initWithStyle:(UITableViewStyle)style
{
  return [self init];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return [[[BNRItemStore sharedStore]allItems]count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


  // Check for a reusable cell first, use that if it exists
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

  // If there is no reusable cell of this type, create a new one
  if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
  } 

  // Set the text on the cell with the description of the item
  // that is at the nth index of items, where n = row this cell
  // will appear in on the tableview
  BNRItem *p = [[[BNRItemStore sharedStore]allItems]objectAtIndex:[indexPath row]];

  [[cell textLabel]setText:[p description]];

  return cell;
}

-(UIView *)headerView
{
  // If we haven't loaded the headerView yet
  if (!headerView) {
    //Load HeaderView.xib
    [[NSBundle mainBundle]loadNibNamed:@"HeaderView" owner:self options:nil];
  }
  return headerView;
}

-(UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)sec
{
  return [self headerView];
}

-(CGFloat)tableView:(UITableView *)tv heightForHeaderInSection:(NSInteger)sec
{
  // The height of the header view should be determined from the height of the 
  // view in the XIB file
  return [[self headerView]bounds].size.height;
}

-(IBAction)toggleEditingMode:(id)sender
{
  // If we are currently in editing mode
  if ([self isEditing]) {
    // Change text of button to inform user of state
    [sender setTitle:@"Edit" forState:UIControlStateNormal];
    // Turn off editing mode
    [self setEditing:NO animated:YES];
  } else {
    // Change text of button to inform user of state
    [sender setTitle:@"Done" forState:UIControlStateNormal];
    // Enter editing mode
    [self setEditing:YES animated:YES];
  }
}

-(IBAction)addNewItem:(id)sender
{
  // Create a new BNRItem and add it to the store
  BNRItem *newItem = [[BNRItemStore sharedStore]createItem];
  //Incompatible pointer types initializing 'BNRItem *__strong' with an expression of type 'NSArray*'

  // Figure out where that item is in the array
  int lastRow = [[[BNRItemStore sharedStore]allItems]indexOfObject:newItem];

  NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRow inSection:0];

  // Insert this new row into the table
  [[self tableView]insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationTop];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
  // If the table view is asking to commit a delete command...
  if(editingStyle == UITableViewCellEditingStyleDelete)
  {
    BNRItemStore *ps = [BNRItemStore sharedStore];
    NSArray *items = [ps allItems];
    BNRItem *p = [items objectAtIndex:[indexPath row]];
    [ps removeItem:p];

    // We also remove that row from the table view with an animation
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  }
}

-(void)tableView:(UITableView *)tableView
  moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath 
         toIndexPath:(NSIndexPath *)destinationIndexPath
{
  [[BNRItemStore sharedStore]moveItemAtIndex:[sourceIndexPath row] 
                                     toIndex:[destinationIndexPath row]];
}

-(void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [[self tableView] reloadData];
}


@end
4

0 回答 0