当用户按下编辑键时,我正在尝试向我的表格 View 中添加带有插入控件(绿色加号)的行。到目前为止,我已经显示了插入行,但是如果用户在编辑模式下尝试滚动 TableView ,应用程序会崩溃并出现以下错误:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[_PFArray objectAtIndex:]: index (9) beyond bounds (9)'
我知道 similar question之前有人问过,但由于我是编程新手,我可能需要更多的帮助。该问题的答案建议确保 numberOfRowsInSection 正确更新。我认为我的是,但我显然在某处犯了错误。
这是我目前在我的 TableView Controller 中得到的,它是我的 UINavigation Controller 的 Root View Controller 。目前它只是一个虚拟表 - 除了编辑按钮之外没有任何连接。
#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
谢谢!
最佳答案
您的部分tableView:cellForRowAtIndexPath:
方法有误
假设您的表格 View 处于编辑模式。并且您在 _trips 中有 10 个对象。
您告诉 TableView 您在数组中有 11 行:
if (tableView.editing) rows++;
并且 tableview 将尝试访问索引为 10 的元素:
Trip *info = [_trips objectAtIndex:indexPath.row];
但是你没有索引为 10 的元素。所以你会得到一个异常
您必须更改为您提供数组索引的逻辑。大概是这样
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;
}
顺便说一句。 if (indexPath.row == !0)
做的事情与 if (indexPath.row != 0)
关于ios - 为什么我的应用程序在我向 TableView 中添加插入行时崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5154516/