Iphone 当我在加载表时尝试选择行时,应用程序崩溃
我在AppDelegate.h中定义数组,如下所示:Iphone 当我在加载表时尝试选择行时,应用程序崩溃,iphone,uitableview,Iphone,Uitableview,我在AppDelegate.h中定义数组,如下所示: NSMutableArray *myWishesArray; . . @property (nonatomic, retain) NSMutableArray *myWishesArray; In AppDelegate.m, i allocate its object like this: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpt
NSMutableArray *myWishesArray;
.
.
@property (nonatomic, retain) NSMutableArray *myWishesArray;
In AppDelegate.m, i allocate its object like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
myWishesArray = [[NSMutableArray alloc] init];
.
.
return YES;
}
-(void)addDataToArray
{
NSMutableArray *tempArray = //Gets data from a web service
if ([tempArray count] > 0) {
[objAppDelegate.myWishesArray addObjectsFromArray:tempArray];
[self.myWishesTableView reloadData];
}
}
假设我有一个函数“向数组添加数据”,如下所示:
NSMutableArray *myWishesArray;
.
.
@property (nonatomic, retain) NSMutableArray *myWishesArray;
In AppDelegate.m, i allocate its object like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
myWishesArray = [[NSMutableArray alloc] init];
.
.
return YES;
}
-(void)addDataToArray
{
NSMutableArray *tempArray = //Gets data from a web service
if ([tempArray count] > 0) {
[objAppDelegate.myWishesArray addObjectsFromArray:tempArray];
[self.myWishesTableView reloadData];
}
}
在MyViewController.m的viewDidLoad中,我将第一组8条记录加载到数组中:
- (void)viewDidLoad {
[self addDataToArray]; //fetches page number 1 of the data to be fetched
}
在MyViewController.m的ScrollViewDiEndDraging中,我添加了下一组8条记录,并使用上述相同的代码将其添加到objappDeleteGate.myWishesArray中:
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
currentPageNumber++;
[self addDataToArray]; //fetches data for next page number
}
-(void)viewWillAppear:(BOOL)animated
{
[self.myWishesTableView reloadData];
}
这是我的cellForRowAtIndexPath方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyObject *myArrayObject = [objAppDelegate.myWishesArray objectAtIndex:indexPath.row];
}
[self.myEngine loadAsynchronousPhotoAvatarInView:objWish.facebookUserId imageView:cell.contentView];
每次我尝试单击在表视图中可见但刚加载完的表行时,我的应用程序就会崩溃,出现以下异常。可能是初始加载,也可能是向下滚动表视图后加载更多行。请记住,当我给表视图足够的时间来处理,运动停止,数据完全加载时,单击加载的行不会遇到任何异常
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
0 CoreFoundation 0x013a8be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x014fd5c2 objc_exception_throw + 47
2 CoreFoundation 0x0139e80c -[__NSArrayI objectAtIndex:] + 236
3 WishHaHa 0x00004e1a -[AsyncImageView image] + 66
4 UIKit 0x0044e56b -[UIImageView setBackgroundColor:] + 101
5 UIKit 0x00549448 -[UITableViewCell _setOpaque:forSubview:] + 953
6 UIKit 0x0054951c -[UITableViewCell _setOpaque:forSubview:] + 1165
7 UIKit 0x0054951c -[UITableViewCell _setOpaque:forSubview:] + 1165
8 UIKit 0x0054472d -[UITableViewCell(UITableViewCellStatic) _updateAndCacheBackgroundColorForHighlightIgnoringSelection:] + 117
9 UIKit 0x0054c12a -[UITableViewCell showSelectedBackgroundView:animated:] + 1435
10 UIKit 0x00548ce3 -[UITableViewCell setHighlighted:animated:] + 231
11 UIKit 0x003fbd3b -[UITableView highlightRowAtIndexPath:animated:scrollPosition:] + 756
12 UIKit 0x003f78d8 -[UITableView touchesBegan:withEvent:] + 1349
13 UIKit 0x004512b8 forwardMethod2 + 92
14 UIKit 0x0054cc15 -[UITableViewCell touchesBegan:withEvent:] + 319
15 UIKit 0x004512b8 forwardMethod2 + 92
16 UIKit 0x00610987 _UIGestureRecognizerSortAndSendDelayedTouches + 3609
17 UIKit 0x006110fc _UIGestureRecognizerUpdateObserver + 927
18 CoreFoundation 0x01389fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
19 CoreFoundation 0x0131f0e7 __CFRunLoopDoObservers + 295
20 CoreFoundation 0x012e7bd7 __CFRunLoopRun + 1575
21 CoreFoundation 0x012e7240 CFRunLoopRunSpecific + 208
22 CoreFoundation 0x012e7161 CFRunLoopRunInMode + 97
23 GraphicsServices 0x01a9d268 GSEventRunModal + 217
24 GraphicsServices 0x01a9d32d GSEventRun + 115
25 UIKit 0x0039442e UIApplicationMain + 1160
26 WishHaHa 0x00002a5c main + 102
27 WishHaHa 0x000029ed start + 53
)
terminate called after throwing an instance of 'NSException'
我对自己的问题做了大量研究,发现引发异常的可能原因可能是:
- 节中的行数:(NSInteger)节;UITableView的 数据源正在传递错误的值
- 应在数组上调用retain
- 无论何时修改表视图的基础数据,都需要 通过调用-[UITableView reloadData]更新更改;或 BeginUpdate和EndUpdate方法,然后添加或删除 正确的索引路径
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomWishCell";
CustomWishCell *cell = (CustomWishCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomWishCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomWishCell *) currentObject;
break;
}
}
}
else
{
AsyncImageView* oldImage = (AsyncImageView*)[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}
if (indexPath.row<[objAppDelegate.myWishesArray count]) {
Wish *objWish = [objAppDelegate.myWishesArray objectAtIndex:indexPath.row];
NSString *test = objWish.facebookUserId;
[self.myEngine loadAsynchronousPhotoAvatarInView:objWish.facebookUserId imageView:cell.contentView];
cell.wishLabel.attributedText = [self.myEngine makeWisherNameAndContents:objWish.firstName lastName:objWish.lastName wishContents:objWish.contents];
}
return cell;
}
AsyncImageView类使用NSURLConnection类加载图像。
请注意,当我对CellForRowatineXpath方法中定义的这一行进行注释时,应用程序运行正常:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyObject *myArrayObject = [objAppDelegate.myWishesArray objectAtIndex:indexPath.row];
}
[self.myEngine loadAsynchronousPhotoAvatarInView:objWish.facebookUserId imageView:cell.contentView];
当我单击正在加载的任何行时,它不会崩溃。请让我知道我应该如何改变我的逻辑。我不允许同步加载图像,或在下载图像时显示任何指示器 在点击tableview之前,加载tableview的数组要么没有被填满,要么在某处被释放 请在释放阵列的位置添加代码,并确保在AppDelegate中合成阵列 我认为数组存在一些问题,而不是tableView,因为数组正在变空
我希望这能有所帮助。在我看来,addDataToArray方法似乎无法在处理单击之前检索数据并填充myWishesArray数组。 您是否从web服务异步检索数据?
有没有办法一次检索所有数据,而不是分段检索?我正在应用程序委托中合成数组,数组确实被填满了。正如我前面提到的,当我给它一些时间安定下来时,它一切正常,没有任何崩溃。如果在我点击tableView之前阵列被释放,我如何定位该点?我已经调试了很多次,但是没有得到它变为空的点。是的。这是我的项目对异步从web服务检索数据的严格要求。无法一次检索所有数据。我必须在用户向下滚动表视图时获取下一个数据。顺便说一句,如果我要求客户端放宽要求,以便我可以一次检索所有数据,那么它会在多大程度上影响我的应用程序的加载时间?取决于数据的大小,但我不认为这是一个大问题。那么,您认为这个问题肯定是因为异步数据加载吗?在给定的场景中,没有解决问题的方法吗?如果这是一个解决方案,那么在某个适当的点保留数组怎么样?我不能在没有看到整个代码的情况下确定,但我相信这就是原因。我不认为这是一个保留/释放问题,因为您没有得到EXC\u BAD\u访问异常
-(NSInteger)tableView:(UITableView*)tableView行数section:(NSInteger)section
方法是什么样的?