代码之家  ›  专栏  ›  技术社区  ›  MartyMcFly

NSTableView reloadData-索引超出界限异常

  •  0
  • MartyMcFly  · 技术社区  · 11 年前

    请原谅我占用了你的时间,但我花了很多时间,尝试了我脑海中的一切,我在网上找到了:/

    问题是在将1条记录添加到NSTableView的数组后重新加载NSTableView。我的应用程序首先扫描目录中的文件,将其名称和修改日期添加到NSDictionary的NSMutableArray中。然后将这些信息加载到NSTableView,一切正常。 但是,当我创建新文件并尝试重新加载NStableViev时,我得到的结果是:

    [__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]
    

    现在我想展示一些我想做的事情的代码。

    下面是我的表如何获取数据

    -(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
        NSLog(@"--------->%ld",(long)filesCount);
        return filesCount;
    }
    
    -(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn
                      row:(NSInteger)row {
        NSDictionary *flag = FilesArray[row];
        NSString *identifier = [tableColumn identifier];
    
        if ([identifier isEqualToString:@"filename"]){
            NSTableCellView *cellview = [tableView makeViewWithIdentifier:@"filename" owner:self];
            [cellview.textField setStringValue:flag[@"name"]];
            return cellview;
    
        }else if ([identifier isEqualToString:@"filedate"]){
            NSTableCellView *cellview = [tableView makeViewWithIdentifier:@"filedate" owner:self];
            [cellview.textField setStringValue:flag[@"date"]];
            return cellview;
        }
        return nil;
    }
    

    下面是我在创建新文件时如何重新加载数据:

    if([identifier isEqualToString:@"newFileModalDone"]){
            NSString *newFileName = [self fileNameModalDone];
    //creating new file 
            [TxtFileObj WriteToFile:[newFileName stringByAppendingString:@".txt"] :nil];
    //rescaning directory
            [TxtFileObj GetFilesList];
            FilesArray = [TxtFileObj GetFileArray];
    //updating files count 
            filesCount = [FilesArray count];
    
            [FilesListTable reloadData];
    
    }
    

    我曾多次尝试观看调试器,似乎异常来自

    NSDictionary *flag = FilesArray[row];
    

    所以我在制作新电影后考虑了错误的阵列大小,但我检查了很多次,一切都很好。

    请帮我找出我做错了什么,如果问题很明显,我对目标C和COCOA很陌生,很抱歉。

    谢谢你的帮助!

    1 回复  |  直到 11 年前
        1
  •  0
  •   MartyMcFly    11 年前

    所以在阅读了一些论坛之后,我终于发现了我的错误:)

    不确定它是否正确,但现在它工作正常,现在代码如下:

    if([identifier isEqualToString:@"newFileModalDone"]){
            NSString *newFileName = [self fileNameModalDone];
            [FilesListTable beginUpdates];
            [TxtFileObj writeToFile:[newFileName stringByAppendingString:@".txt"] :nil];
            [TxtFileObj getFilesList];
            FilesArray = [TxtFileObj filesListArray];
            [FilesListTable insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:[FilesArray count]-1]
                                  withAnimation:NSTableViewAnimationSlideDown];
    
            [FilesListTable endUpdates];
            [FilesListTable reloadData];
        }
    

    我基本上做的是添加3种方法:

    [FilesListTable beginUpdates];
    
    [FilesListTable insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:[FilesArray count]-1]
                                      withAnimation:NSTableViewAnimationSlideDown];
    
    [FilesListTable endUpdates];