Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
Iphone NSMutableArray和my表的问题_Iphone_Xcode_Cocoa_Uitableview_Nsmutablearray - Fatal编程技术网

Iphone NSMutableArray和my表的问题

Iphone NSMutableArray和my表的问题,iphone,xcode,cocoa,uitableview,nsmutablearray,Iphone,Xcode,Cocoa,Uitableview,Nsmutablearray,我试图将一个对象插入到我的表中,但每当我点击add按钮时,它就会崩溃,因为出现了一个未捕获的异常。上面说 Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update 这是我的主视图控制器,它处理我的所有表格内容 #import "MasterViewController.h" #import "DetailViewController.h" #define k

我试图将一个对象插入到我的表中,但每当我点击add按钮时,它就会崩溃,因为出现了一个未捕获的异常。上面说

Attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update
这是我的主视图控制器,它处理我的所有表格内容

#import "MasterViewController.h"

#import "DetailViewController.h"

#define kFileName       @"steakNames.plist"

@interface MasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation MasterViewController

@synthesize steakName;

- (NSString *)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFileName];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Master", @"Master");
    }
    return self;
}

- (void)viewDidLoad
{
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        _objects = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
    }

    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];

    [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;
}

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)insertNewObject:(id)sender
{
    if (!steakName) {
        steakName = @"No name";
    }
    [_objects insertObject:[self steakName] atIndex:0];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _objects.count;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    NSDate *object = [_objects objectAtIndex:indexPath.row];
    cell.textLabel.text = [object description];
    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) {
        [_objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[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
{
    if (!self.detailViewController) {
        self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    }
    NSDate *object = [_objects objectAtIndex:indexPath.row];
    self.detailViewController.detailItem = object;
    [self.navigationController pushViewController:self.detailViewController animated:YES];
}

- (void)applicationWillResignActive:(NSNotification *)notification {
    [_objects writeToFile:[self dataFilePath] atomically:YES];
}
@end

最可能的错误原因是返回0:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return ...;
}
我建议您首先使用表视图的完整工作示例及其所有必需的方法,然后从那里开始

通常,您需要实现以下方法:

  • (NSInteger)表格视图中的节数:(UITableView*)表格视图

  • (NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分

  • (NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节

  • (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

  • (void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath


到目前为止,任何NSMutableArray都没有问题,但是如果数组还没有足够数量的对象,则不能在索引处插入对象,例如在索引4处插入,但数组只有2个对象。您始终可以将对象添加到NSMutableArray,并增加此数组中的对象数。

您是否也覆盖了
numberofSectionsInTableView
?你能为点击add按钮时发生的情况发布一些代码吗?你试图添加的代码在哪里?@DanF我把它放在那里了,我将它设置为1。这与使用可变数组无关——事实上,如果你想从表中添加和删除内容,你应该使用可变数组。如果您需要这方面的帮助,您需要发布更多的代码。显示加载表的代码可能会有所帮助。以及填充数组的方法的完整代码。你读过我的问题吗?我明确地说我在那里有这个。你检查过这个方法返回什么吗?“添加”按钮到底是做什么的?问题出在你没有在这里显示的代码中!