0

私のアプリは正常に実行されていました。最初のテスト時のようにメインのUIクラスで処理されるデータではなく、専用のデータコントローラークラスを持つようにアプリを変更していません。ただし、変更後、テーブルビューに新しいアイテムを追加するとクラッシュし続けます。

クラッシュするコードの行とエラーは次のとおりです。

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

2012-07-22 07:17:44.772 Speecher [1897:707] *キャッチされなかった例外'NSInternalInconsistencyException'が原因でアプリを終了しました。理由:'セクション0に行0を挿入しようとしましたが、セクション0の後に0行しかありません。アップデート'

そのクラス(メインのMasterViewControllerクラス)の完全なコードは次のとおりです。

//
//  SpeecherMasterViewController.m
//  speecher
//
//

#import "SpeecherMasterViewController.h"
#import "SpeecherDataController.h"
#import "SpeecherDetailViewController.h"

@interface SpeecherMasterViewController () {

    NSString *newTitle;
    NSMutableArray *_speeches;
    NSMutableArray *_content;
    SpeecherDataController *object;

}
@end

@implementation SpeecherMasterViewController

@synthesize detailViewController = _detailViewController;

- (void)awakeFromNib
{
    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
    self.detailViewController = (SpeecherDetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
    object = [[SpeecherDataController alloc] init];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}


#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [object returnNoObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text = [object returnTitle:indexPath.row];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_speeches removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    NSString *titleobj = [object returnTitle:indexPath.row];
    NSString *contentobj = [object returnContent:indexPath.row];
    self.detailViewController.detailItem = titleobj;
    self.detailViewController.detaitContent = contentobj;
}



- (void)insertNewObject:(id)sender
{
    //Make sure clear before we start, also make sure initalized (double redundancy with clear statement at end)
    newTitle = @"";

    //New Title pop up UIAlert View
    UIAlertView * alert = [[UIAlertView alloc] 
                           initWithTitle:@"New Speech" 
                           message:@"Please enter a name for speech" 
                           delegate:self 
                           cancelButtonTitle:@"Create" 
                           otherButtonTitles:nil];

    alert.alertViewStyle = UIAlertViewStylePlainTextInput;

    UITextField * alertTextField = [alert textFieldAtIndex:0];

    alertTextField.keyboardType = UIKeyboardTypeDefault;

    alertTextField.placeholder = @"Enter a new title";
    [alert show];


}



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ 
    newTitle = [[alertView textFieldAtIndex:0] text];

    [object addNewContent:newTitle :@"IT REALLY WORKS!" :@"Nothing"];

        //create new speech title, add to array and add to tableview
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    //Clear newTitle for use next time
    newTitle = @"";

}

@end

編集:

[object addNewContent]コメントに従ってメソッドとクラスを追加するように修正されました。

//
//  SpeecherDataController.m
//  speecher
//
//

#import "SpeecherDataController.h"

@interface SpeecherDataController ()
{
    NSMutableArray *titles;
    NSMutableArray *content;
    NSMutableArray *timer;
}
@end

@implementation SpeecherDataController

-(void) addNewContent:(NSString*)sTitle : (NSString*)sContent :(NSString*)sTimer
{
    [titles insertObject:sTitle atIndex:0];
    [content insertObject:sContent atIndex:0];
    [timer insertObject:sTimer atIndex:0];
}


//Methods to return data
-(NSString*) returnTitle:(NSUInteger)row
{
    return [titles objectAtIndex:row];
}

-(NSString*) returnContent:(NSUInteger)row
{
    return [content objectAtIndex:row];
}

-(NSString*) returnTimer:(NSUInteger)row
{
    return [timer objectAtIndex:row];
}

-(NSInteger) returnNoObjects
{
    return titles.count;
}



@end
4

1 に答える 1

1

問題は、NSMutableArraysがallocおよびinitされていなかったことです。initがあるかどうかを確認するためのチェックを追加し、ない場合はallocする必要がありました。新しいチェックは次のようになります、

-(void) addNewContent:(NSString*)sTitle : (NSString*)sContent :(NSString*)sTimer
{

    if(!titles)
    {
        titles = [[NSMutableArray alloc] init];
    }
    if(!content)
    {
        content = [[NSMutableArray alloc] init];
    }
    if(!timer)
    {
        timer = [[NSMutableArray alloc] init];
    }

    [titles insertObject:sTitle atIndex:0];
    [content insertObject:sContent atIndex:0];
    [timer insertObject:sTimer atIndex:0];
}
于 2012-07-22T07:11:05.283 に答える