English 中文(简体)
缩略语 英文 目标指数
原标题:iOS NSMutableArray insertObject index out of bound

在目标C的方案拟订中,我对主题问题感到非常担忧。

我试图撰写一些简单的材料,在表层中增加储存的单一数字。

此处是“附加系统”方法的内置代码和代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"Test App";
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd   target:self action:@selector(addItem)];
    self.navigationItem.rightBarButtonItem = addButton;

    self.itemArray = [[NSMutableArray alloc] initWithCapacity:100];
}

- (void)addItem {
    [itemArray insertObject:@"1" atIndex:0];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

这一条

[itemArray insertObject:@"1" atIndex:0];

产生以下错误:

*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array 

为什么指数0超出空阵的界限,我干什么错?

<><>UPD>/strong>

正如BP指出的,插入阵列可能不利于空阵,因此我补充说:

if ([itemArray count] == 0) {
    [itemArray addObject:@"1"];     
} else {
    [itemArray insertObject:@"1" atIndex:0];
}

因此,我现在收到同样的错误信息,但行文不变。

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

任何关于可能有什么错误的想法?

最佳回答

电话:insertRowsAtIndexPaths:with Row Animation: 应在<条码>的起首日期与<条码>上>。 电话: View

查阅)。

问题回答

惯用能力法只为这些物体预留记忆,实际上并不造成你阵列中指明的物体数目。

我不敢肯定,你能否插入一个阵列,但没有任何物体,因此,你可能希望利用这一添加。 如果阵列数为0,则目标方法改为 否则,反对方法。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签