在我的刷新方法中,我使用以下代码滚动到顶部,它工作正常,但是当我在第三个单元格后向下滚动并再次点击刷新时,它会崩溃应用程序,错误终止,类型为未捕获异常NSException
。
- (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;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger total;
if ([catName isEqualToString:@"All"]) {
total = imageArray.count;
}
else
{
total = imageCatArray.count;
}
return total;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
UITableView
有7个动态单元格。我是否需要添加一些东西以使其适用于整个tableview?
完整的错误消息:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array' *** First throw call stack: (0x189516f50 0x195a201fc 0x1894184b0 0x1000984d0 0x18c628904 0x18c5c6db0 0x18c5c662c 0x18c4daff8 0x18c0cc258 0x18c0c6e20 0x18c0c6cd8 0x18c0c6560 0x18c0c6304 0x18c11cb84 0x18a417e74 0x1894c88e0 0x1894d6e90 0x1894d6df0 0x1894d5014 0x189415c20 0x18f0fdc0c 0x18c546fdc 0x10008cedc 0x196013aa0) libc++abi.dylib: terminating with uncaught exception of type NSException
答案 0 :(得分:0)
尝试将所有NSMutableArray alloc移入viewDidLoad方法。发生的事情是,每次刷新时都会重新初始化数组,这会刷新第一个并且只给它一个可用的索引。