刷新UITableView在第三个单元格后崩溃后滚动到顶部

在我的刷新方法,我使用下面的代码滚动到顶部工作正常,但是当我向下滚动第三个单元格后,再次点击刷新它崩溃的应用程序error terminating with uncaught exception of type NSException

 [TableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES]; 

UITableView有7个dynamic单元格。 我需要添加一些东西,使其整个tableview的工作?

我在StackOverflow上search了一个解决scheme,但不幸的是他们没有解决我的问题。

你的cellForRowAtIndexPath函数试图访问一个超出其范围的数组对象,如错误所述。 我猜你有一个数组正在更新不同的对象,大小并不总是相同的,如果是数组并不总是充满对象。

在做这样的事情之前,我会build议:

 cell.myCellTitle.text = [yourArrayVariable objectAtIndex:indexPath.row]; 

要访问您的数据,请首先检查数组的数量是否大于您的indexPath.row cellForRowAtIndexPath正试图调用。

 if (yourArrayVariable.count > indexPath.row) { cell.myCellTitle.text = [yourArrayVariable objectAtIndex:indexPath.row] } 

这样,每当你尝试重新加载你的tableView,你肯定至less该函数不会超越NSArray的界限。

还有一件事,你应该小心你何时何地试图定义数组,以及如何初始化它。 如果每次刷新时重新载入tableView,那么您可能会将一些新数据分配给该数组,您应该考虑在使用数据正确初始化arrays后重新载入数据。

也许一些片段可以帮助您查看viewDidLoad,刷新操作和cellForRowAtIndexPath委托的function。 这也将有助于查看您的应用程序的性质以及您在该UITableView控制器中要做什么。 像从远程服务器获取数据,并使用dispatch_queue_t,并从dispatch_async(dispatch_get_main_queue())调用您的重新加载。

如果你需要更多的细节,请打我评论,我会更新这个答案,因为你发展你的问题。

有快乐的编码:)

干得好…

 - (void)viewDidLoad { [super viewDidLoad]; NewsTbView.delegate = self; NewsTbView.dataSource = self; [self refresh]; } -(void)refresh { catName = @"All"; self.imageArray = [[NSMutableArray alloc]init]; self.titleArray = [[NSMutableArray alloc]init]; self.descArray = [[NSMutableArray alloc]init]; self.dateArray = [[NSMutableArray alloc]init]; self.linkArray = [[NSMutableArray alloc]init]; self.categoryArray = [[NSMutableArray alloc]init]; self.categoryNewsArray = [[NSMutableArray alloc]init]; self.imageCatArray = [[NSMutableArray alloc]init]; self.titleCatArray = [[NSMutableArray alloc]init]; self.descCatArray = [[NSMutableArray alloc]init]; self.dateCatArray = [[NSMutableArray alloc]init]; self.linkCatArray = [[NSMutableArray alloc]init]; // Create the request. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[config getXmlWordPress]]]; // Create url connection and fire request conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.mode = MBProgressHUDModeIndeterminate; hud.labelText = @"Verversen"; hud.dimBackground = YES; [NewsTbView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIndent"; TableViewCellHome *cell = (TableViewCellHome *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; NSString *CellName; CellName = @"TableViewCellHome"; if (cell == nil) { UIViewController *c = [[UIViewController alloc] initWithNibName:CellName bundle:nil]; cell = (TableViewCellHome *)c.view; } cell.textLabel.numberOfLines = 0; [cell.txtDate setAlpha:0]; [cell.txtTitle setAlpha:0]; [cell.btnShare setAlpha:0]; [cell.imgNews setAlpha:0]; [UIView beginAnimations:@"CustomCellAnimation" context:NULL]; [UIView setAnimationDuration:1.0f]; [cell.txtDate setAlpha:1]; [cell.txtTitle setAlpha:1]; //[cell.btnShare setAlpha:1]; [cell.imgNews setAlpha:1]; [UIView commitAnimations]; if ([catName isEqualToString:@"All"]) { cell.txtTitle.text = [titleArray objectAtIndex:indexPath.row]; cell.txtDate.text = [dateArray objectAtIndex:indexPath.row]; [cell.imgNews setImage:[imageArray objectAtIndex:indexPath.row]]; [cell.btnShare addTarget:self action:@selector(sharePost:) forControlEvents:UIControlEventTouchUpInside]; cell.btnShare.tag = indexPath.row; } else { cell.txtTitle.text = [titleCatArray objectAtIndex:indexPath.row]; cell.txtDate.text = [dateCatArray objectAtIndex:indexPath.row]; [cell.imgNews setImage:[imageCatArray objectAtIndex:indexPath.row]]; [cell.btnShare addTarget:self action:@selector(sharePost:) forControlEvents:UIControlEventTouchUpInside]; cell.btnShare.tag = indexPath.row; } return cell; }