Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在TableView InsertRowstIndeXPath上崩溃_Ios_Uitableview_Ios5_Crash - Fatal编程技术网

Ios 在TableView InsertRowstIndeXPath上崩溃

Ios 在TableView InsertRowstIndeXPath上崩溃,ios,uitableview,ios5,crash,Ios,Uitableview,Ios5,Crash,我的应用程序运行良好,我没有将其修改为具有专用的数据控制器类,而不是像初始测试时那样在主UI类中处理数据。但是,自从更改之后,当向tableview添加新项时,它会不断崩溃 代码行及其崩溃的错误是 [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 2012-07-22 07:17:44.772

我的应用程序运行良好,我没有将其修改为具有专用的数据控制器类,而不是像初始测试时那样在主UI类中处理数据。但是,自从更改之后,当向tableview添加新项时,它会不断崩溃

代码行及其崩溃的错误是

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
2012-07-22 07:17:44.772演讲者[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

问题是NSMutableArray没有被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];
}

我看不到您提供的代码中有任何错误,但是查看您收到的消息以及“tableView:numberOfRowsInSection:”方法,我猜您的“[object returnNoObjects]”方法返回的行数不正确(在本例中为0)。@NSArray您几乎是对的。在查看时,我发现
[object addNewContent]
函数实际上并没有添加内容。修改问题以显示该函数。添加对象后,请尝试打印“标题”数组的值。如果没有分配,你会想按照詹姆斯克劳的建议去做。谢谢你的帮助,但我是詹姆斯克劳。我解决了这个问题,所以自己回答这个问题:P