Objective c iOS-仅在禁用断点时发生异常
最近开始开发应用程序,请原谅我的无知。我有一个表视图,当单击表视图中的单元格时,我想在其正下方插入一个新行。这目前在我的代码中起作用。但是,我还希望在再次单击单元格后删除插入的行。这给了我一个异常,表示我在数组中超出了边界 我想这可能与我的tableView委托/数据方法有关,所以我在每个方法上都设置了断点。启用断点后,单元格将完全删除。但是,当我禁用断点并让应用程序自己运行时,它会崩溃。断点是如何影响这一点的 以下是相关代码:Objective c iOS-仅在禁用断点时发生异常,objective-c,ios5,uitableview,nsrangeexception,Objective C,Ios5,Uitableview,Nsrangeexception,最近开始开发应用程序,请原谅我的无知。我有一个表视图,当单击表视图中的单元格时,我想在其正下方插入一个新行。这目前在我的代码中起作用。但是,我还希望在再次单击单元格后删除插入的行。这给了我一个异常,表示我在数组中超出了边界 我想这可能与我的tableView委托/数据方法有关,所以我在每个方法上都设置了断点。启用断点后,单元格将完全删除。但是,当我禁用断点并让应用程序自己运行时,它会崩溃。断点是如何影响这一点的 以下是相关代码: - (NSInteger) numberOfSectionsInT
- (NSInteger) numberOfSectionsInTableView:(UITableView *)songTableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)songTableView
numberOfRowsInSection:(NSInteger)section{
bool debug = false;
if (debug) NSLog(@"--TableView: rankings");
if (expandedRow == -1)
return [self.songs count];
else //one row is expanded, so there is +1
return ([self.songs count]+1);
}
- (UITableViewCell *)tableView:(UITableView *)songTableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
bool debug = false;
if (debug) NSLog(@"--tableView: tableView");
NSUInteger row = [indexPath row];
if (row == expandedRow){ //the expanded row, return the custom cell
UITableViewCell *temp = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"test"];
return temp;
}
UITableViewCell *cell = [tableViewCells objectAtIndex:row];
return cell;
}
}
- (NSString *)tableView:(UITableView *)songTableView
titleForHeaderInSection:(NSInteger)section{
//todo: call refresh title
return @"The Fresh List";
}
- (CGFloat)tableView:(UITableView *)songTableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 44.0; //same as SongCell.xib
}
- (void)tableView: (UITableView *)songTableView
didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
bool debug = true;
//todo: if the user selects expanded cell, doesn't do anything
SongCell *cell = (SongCell *)[songTableView cellForRowAtIndexPath:indexPath];
if (cell->expanded == NO){
//change cell image
cell.bgImage.image = [UIImage imageNamed:@"tablecellbg_click.png"];
cell->expanded = YES;
//add new cell below
NSInteger atRow = [indexPath row] + 1;
NSIndexPath *insertAt = [NSIndexPath indexPathForRow:atRow inSection:0];
NSArray *rowArray = [[NSArray alloc] initWithObjects:insertAt, nil];
if (debug) NSLog(@"Expanded row: %d", atRow);
expandedRow = atRow;
[tableView insertRowsAtIndexPaths:rowArray withRowAnimation:UITableViewRowAnimationTop];
}else { //cell is already open, so close it
//change cell image
cell.bgImage.image = [UIImage imageNamed:@"tablecellbg.png"];
cell->expanded = NO;
NSIndexPath *removeAt = [NSIndexPath indexPathForRow:expandedRow inSection:0];
NSArray *rowArray = [[NSArray alloc] initWithObjects:removeAt, nil];
if(debug) NSLog(@"--about to delete row: %d", expandedRow);
expandedRow = -1;
[tableView deleteRowsAtIndexPaths:rowArray withRowAnimation:UITableViewRowAnimationTop];
//remove expaned cell below
}
}
我打赌这将返回null:[NSIndexPath indexPathForRow:expandedRow片段:0];如果它真的吹了
hth这只是一个猜测,但是包装代码以更改调用中的表结构是一个好主意
[tableView beginUpdates];
[tableView endUpdates];
我不想回答我自己的问题,但我明白了 我正在从一组对象加载我的tableView。当我添加单元格时,该数组仍然只能容纳30个对象,而我的表只能容纳31个对象。我正确地返回了numberOfRowsInSection方法 这是我做的修改。如果出现以下情况,请注意额外的else:
NSUInteger row = [indexPath row];
if (row == expandedRow){ //the expanded row, return the custom cell
if(debug) NSLog(@"row == expandedRow");
UITableViewCell *temp = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"test"];
return temp;
}
else if (expandedRow != -1 && row > expandedRow)
row--;
我的对象数组和UITableViewCells应该匹配1-1。在展开的行之后,indexPath的行因为关闭了1。这是我对这个问题的快速解决方法,尽管我确信有更好的方法来解决这个问题。不,不是这个,我在方法的顶部放了一个NSLog。结果是它在调用DidSelectRow之前就崩溃了。。。