我的应用程序中有一个可编辑的UITableView(添加和删除元素)。它的工作很奇怪。
虽然我只有一两行,但一切都很完美。但如果我添加更多项目,我有一个例外'*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
我无法设置断点并处理它。也许,有人可以帮助我吗?
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int plusRow = 0;
    if ([[self tableView] isEditing]) plusRow = 1;
    return [typeList count] + plusRow;
}
- (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];
    }
    if ([indexPath row] > ([typeList count]-1)) {
        NSString * appendCellDescription = @"";
        if ([[self tableView] isEditing]) appendCellDescription = @"Add";
        [[cell textLabel] setText:appendCellDescription];
    } else {
        NSLog(@"Accessing array: %d", [indexPath row]);
        [[cell textLabel] setText:[[typeList getObject:[indexPath row]] description]];
    }
    return cell;
}
#pragma mark - Editing table view
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"Row: %d, count: %d", [indexPath row], [typeList count]);
    if (indexPath.row > [typeList count]-1) {
        return UITableViewCellEditingStyleInsert;
    } else {
        return UITableViewCellEditingStyleDelete;
    }
}
- (IBAction)btnModifyClick:(id)sender{
    if ([[self tableView] isEditing]) {
        [[self tableView] setEditing:FALSE animated:YES];
        [[self tableView] reloadData];
    } else {
        [[self tableView] setEditing:TRUE animated:YES];
        [[self tableView] reloadData];
    }
}