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”而终止app,原因:“* -[_PFArray objectAtIndex:]:索引(9)越界(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. I think mine is, but I am obviously making a mistake somewhere.
我知道以前也有人问过类似的问题,但由于我是编程新手,我可能需要更多的人手。这个问题的答案建议确保numberOfRowsInSection进行了正确的更新。我想是我的,但很明显我在什么地方犯了一个错误。
This is what I've got so far in my table view controller, which is the root view controller for my UINavigation Controller. At the moment it's just a dummy table - nothing is hooked up except the edit button.
这是到目前为止我在表视图控制器中得到的,它是UINavigation控制器的根视图控制器。目前,它只是一个虚拟表——除了编辑按钮之外,什么都没有连接。
#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!
谢谢!
2 个解决方案
#1
3
parts of your tableView:cellForRowAtIndexPath:
method are wrong
tableView的部分:cellForRowAtIndexPath:方法是错误的
Imagine your tableview is in editingmode. and you have 10 objects in _trips.
假设您的tableview处于编辑模式。在_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)
顺便说一句。如果(indexPath。行== !0)与if (indexPath)不同。行! = 0)
#2
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
这意味着在数组中存储的行数是问题所在。请检查数组。崩溃是因为数组的元素总数超过了数组的限制
#1
3
parts of your tableView:cellForRowAtIndexPath:
method are wrong
tableView的部分:cellForRowAtIndexPath:方法是错误的
Imagine your tableview is in editingmode. and you have 10 objects in _trips.
假设您的tableview处于编辑模式。在_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)
顺便说一句。如果(indexPath。行== !0)与if (indexPath)不同。行! = 0)
#2
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
这意味着在数组中存储的行数是问题所在。请检查数组。崩溃是因为数组的元素总数超过了数组的限制