简体   繁体   English

当我向表格视图添加插入行时,为什么我的应用程序崩溃了?

[英]Why does my app crash when I add an insert row to my table view?

I am trying to add a row with an insert control (green plus) to my table view when the user presses edit. 当用户按下编辑时,我正在尝试向表格视图添加一个带有插入控件(绿色加号)的行。 So far, I've got the insert row to show, but if the user tries to scroll the table view while it is in edit mode, the app crashes with the following error: 到目前为止,我已经显示了插入行,但是如果用户在编辑模式下尝试滚动表视图,则应用程序崩溃并出现以下错误:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[_PFArray objectAtIndex:]: index (9) beyond bounds (9)' *由于未捕获的异常'NSRangeException'终止应用程序,原因:'* - [_ PFArray objectAtIndex:]:index(9)超出bounds(9)'

I know that a similar question has been asked before, but as I am new to programming I might need a bit more handholding. 我知道之前已经提出了类似的问题 ,但由于我是编程新手,我可能需要更多的支持。 The answer to that question suggested making sure that numberOfRowsInSection was updating properly. 该问题的答案建议确保numberOfRowsInSection正确更新。 I think mine is, but I am obviously making a mistake somewhere. 我认为我的是,但我显然在某个地方犯了错误。

This is what I've got so far in my table view controller, which is the root view controller for my UINavigation Controller. 这是我目前在我的表视图控制器中所拥有的,它是我的UINavigation控制器的根视图控制器。 At the moment it's just a dummy table - nothing is hooked up except the edit button. 目前它只是一个虚拟表 - 除了编辑按钮之外没有任何东西被连接起来。

#import "RootViewController.h"
#import "AppDelegate_iPhone.h"
#import "Trip.h"

@implementation RootViewController
@synthesize trips = _trips;
@synthesize context = _context;


- (id)init
{
    self = [super init] ;
    if (self)
    {
         automaticEditControlsDidShow = NO;
    }
    return self ;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Trip" inManagedObjectContext:_context]];   
    NSError *error;
    self.trips = [_context executeFetchRequest:fetchRequest error:&error];
    self.title = @"Trips";
    [fetchRequest release];

    // Display an Edit button for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;   
}


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


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

    int rows = [_trips count];
    if (tableView.editing) rows++;
    return rows;
}


- (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] autorelease];
    }

    // Configure the cell...
    Trip *info = [_trips objectAtIndex:indexPath.row];
    if (tableView.editing)
    {
        if (indexPath.row == 0)
            cell.textLabel.text = @"Add New Trip";
        if (indexPath.row == !0)
            cell.textLabel.text = info.tripName;
    }
    else
    {
        cell.textLabel.text = info.tripName;
    }
    return cell;    
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    int row = indexPath.row;

    if (self.editing && row == 0) {
        if (automaticEditControlsDidShow)
            return UITableViewCellEditingStyleInsert;
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleDelete;
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    automaticEditControlsDidShow = NO;
    [super setEditing:editing animated:animated];

    NSArray *addRow = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0],nil];
    [self.tableView beginUpdates];
    if (editing) {
        automaticEditControlsDidShow = YES;
        [self.tableView insertRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft];
    } else {
        [self.tableView deleteRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft];
    }
    [self.tableView endUpdates];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (void)dealloc {
    [_trips release];
    [_context release];
    [super dealloc];
}


@end

Thanks! 谢谢!

parts of your tableView:cellForRowAtIndexPath: method are wrong 你的tableView:cellForRowAtIndexPath:方法是错误的

Imagine your tableview is in editingmode. 想象一下你的tableview处于编辑模式。 and you have 10 objects in _trips. 并且_trips中有10个对象。

You tell the tableview that you have 11 rows in the array: 你告诉tableview你在数组中有11行:

if (tableView.editing) rows++;

And the tableview will try to access element with index 10 here: 并且tableview将尝试访问索引为10的元素:

Trip *info = [_trips objectAtIndex:indexPath.row];

But you don't have an element with index 10. So you'll get an exception 但是你没有索引为10的元素。所以你会得到一个例外

You have to change the logic that gives you the index in the array. 您必须更改为您提供数组中索引的逻辑。 Maybe like this 也许是这样的

if (tableView.editing) 
{   // tableview row count is _trips count + 1
    if (indexPath.row == 0)
        cell.textLabel.text = @"Add New Trip";
    if (indexPath.row != 0) {
        // realIndex = table view index - 1 
        Trip *info = [_trips objectAtIndex:indexPath.row - 1];
        cell.textLabel.text = info.tripName;
    }
}
else
{
    Trip *info = [_trips objectAtIndex:indexPath.row];
    cell.textLabel.text = info.tripName;
}

and btw. 顺便说一句。 if (indexPath.row == !0) does something different than if (indexPath.row != 0) if (indexPath.row == !0)做的不同于if (indexPath.row != 0)

It means the number of rows which you are storing in an array is the problem. 这意味着您存储在数组中的行数是问题。 Pls check the array. 请检查阵列。 The crash is because the total number of elements for the array has exceeded the limits of the array 崩溃是因为数组的元素总数超出了数组的限制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么更换框架时我的应用有时会崩溃? - Why does my app occasionally crash when I change a frame? 为什么在我调用objectForInfoDictionaryKey:CFBundleShortVersionString时我的应用程序崩溃 - why does my app crash when I call for objectForInfoDictionaryKey: CFBundleShortVersionString 为什么当我的masterview控制器尝试启动我的详细信息视图时,我的应用程序崩溃? - why does my app crash when my masterview controller tries to launch my detail view? 为什么我的空 iOS 单视图应用程序项目崩溃? - Why does my empty iOS single view app project crash? 当我从Swift的音乐库中选取一首歌曲时,为什么我的应用程序崩溃? - Why does my app crash when I pick a song from my music library in Swift? 尝试presentModalViewController时,为什么我的iOS应用程序崩溃? - Why does my iOS app crash when trying presentModalViewController? 为什么我的应用程序在尝试登录时崩溃(firebase 登录) - Why does my app crash when trying to login (firebase login) 为什么收到推送通知时我的iOS应用程序崩溃? - Why does my iOS app crash when receiving a push notification? 为什么我的 iOS 应用打开后突然崩溃? - Why does my iOS app suddenly start to crash when opened? 当我尝试访问联系人的姓名时,为什么我的应用程序崩溃? - Why does my app crash when I try to access a contact’s name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM