Iphone 以编程方式填充的TableView错误

Iphone 以编程方式填充的TableView错误,iphone,objective-c,ios,nsmutablearray,Iphone,Objective C,Ios,Nsmutablearray,向下滚动表格视图时出现此错误: 2012-04-23 09:32:36.763 RedFox[30540:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 12 beyond bounds [0 .. 11]' *** First throw call stack: (0x13c3052 0x1554d0a

向下滚动表格视图时出现此错误:

2012-04-23 09:32:36.763 RedFox[30540:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 12 beyond bounds [0 .. 11]'
*** First throw call stack:
(0x13c3052 0x1554d0a 0x13af674 0x5794 0xb3e0f 0xb4589 0x9fdfd 0xae851 0x59322 0x13c4e72 0x1d6d92d 0x1d77827 0x1cfdfa7 0x1cffea6 0x1cff580 0x13979ce 0x132e670 0x12fa4f6 0x12f9db4 0x12f9ccb 0x12ac879 0x12ac93e 0x1aa9b 0x2158 0x20b5)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
Current language:  auto; currently objective-c
在我的.h文件中,我有:

@interface MyTableView : UIViewController  <UITableViewDataSource> {
    int currentRow;
}

@property (strong,nonatomic) UITableView *tableView;
@property (strong,nonatomic) ViewBuilder *screenDefBuild;

这有什么问题?

currentRow变量是不必要的,会导致问题

要修复:

换线

 ScreenListElements *currentScreenElement = [screenDefBuild.elementsToTableView objectAtIndex:currentRow]; //exception points here!

原因:


每次调用
CellforRowAtIndexPath:
时,您都会增加
currentRow
,这是错误的,因为中的此方法不仅在显示以下单元格(向下滚动时)时调用,而且在向上滚动时也会调用(因此
currentRow
应该减小)。这就是为什么Apple将参数
indepath
放在一起,这样您就可以轻松地确定哪个单元格tableView正在请求。

currentRow变量是不必要的,并且会导致问题

要修复:

换线

 ScreenListElements *currentScreenElement = [screenDefBuild.elementsToTableView objectAtIndex:currentRow]; //exception points here!

原因:


每次调用
CellforRowAtIndexPath:
时,您都会增加
currentRow
,这是错误的,因为中的此方法不仅在显示以下单元格(向下滚动时)时调用,而且在向上滚动时也会调用(因此
currentRow
应该减小)。这就是为什么苹果将参数
indepath
,这样你就可以很容易地确定是哪个单元格tableView请求的。

当前行对我来说没有意义。 返回包含源数组行的单元格(elementsToTableView) 您需要请求当前索引路径中的行

导致错误的行应如下所示:

ScreenListElements *currentScreenElement = [screenDefBuild.elementsToTableView objectAtIndex: indexPath.row];
您根本不需要currentRow。
为什么要用这种方式实现它?

当前行对我来说没有意义。 返回包含源数组行的单元格(elementsToTableView) 您需要请求当前索引路径中的行

导致错误的行应如下所示:

ScreenListElements *currentScreenElement = [screenDefBuild.elementsToTableView objectAtIndex: indexPath.row];
您根本不需要currentRow。
为什么要用这种方式实现它?

感谢您的解决方案,更感谢您的解释。它起作用了!感谢您的解答,更感谢您的解释。它起作用了!