在我的刷新方法中,我使用以下代码滚动到顶部,工作正常,但是当我在第三个单元格后向下滚动并再次点击刷新时,它会使用error terminating with uncaught exception of type NSException
崩溃应用程序。
[TableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
UITableView
有7个动态单元格。我是否需要添加一些东西以使其适用于整个tableview?
我在StackOverflow上搜索了一个解决方案,但遗憾的是没有一个解决了我的问题。
答案 0 :(得分:0)
您的cellForRowAtIndexPath函数尝试访问超出其边界的数组对象,如错误所示。我猜你有一个正在使用不同对象进行更新的数组,并且大小并不总是相同,或者如果数组并不总是满是对象。
在做这样的事情之前我会建议:
cell.myCellTitle.text = [yourArrayVariable objectAtIndex:indexPath.row];
要访问您的数据,首先检查数组的计数是否大于您尝试调用的cellForRowAtIndexPath的indexPath.row。
if (yourArrayVariable.count > indexPath.row) {
cell.myCellTitle.text = [yourArrayVariable objectAtIndex:indexPath.row]
}
这样,无论何时尝试重新加载tableView,您都可以确定该函数至少不会超出NSArray范围。
还有一件事,你应该注意你何时何地尝试定义数组以及如何初始化它。如果每次点击刷新时重新加载tableView,那么您可能正在为数组分配一些新数据,并且在使用数据正确初始化数组后应考虑重新加载。
也许某些代码段有助于查看您在viewDidLoad,刷新操作和cellForRowAtIndexPath委托中执行的操作。它还有助于查看应用程序的性质以及您在UITableView控制器中尝试执行的操作。比如从远程服务器获取数据并使用dispatch_queue_t并从dispatch_async调用重新加载(dispatch_get_main_queue()。
如果您需要更多详细信息,请发表评论,我会在您提出问题时更新此答案。
快乐编码:)
答案 1 :(得分:0)
你去......
- (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;
}