错误时如子视图中的UIViewController
的UITableViewController添加我做一个简单的应用程序,查看项目从JSON服务器错误时如子视图中的UIViewController
我用UISegment在一个UIViewController中添加不同的子视图
- (IBAction)segmentSwitch:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
if (selectedSegment == 0) {
instantiateViewControllerWithIdentifier:@"prestige"] animated:NO];
UIViewController *subController = [self.storyboard instantiateViewControllerWithIdentifier:@"prestige"];
[mainView addSubview:subController.view];
}
else if(selectedSegment == 1)
{
instantiateViewControllerWithIdentifier:@"latest"]];
//[self setView: [self.storyboard instantiateViewControllerWithIdentifier:@"latest"]];
//UIViewController *subController = [self.storyboard instantiateViewControllerWithIdentifier:@"latest"];
//[mainView addSubview:subController.view];
UITableViewController *tableVC =[self.storyboard instantiateViewControllerWithIdentifier:@"latest"];
[self.view addSubview:tableVC.tableView];
}
else if (selectedSegment == 2)
{
instantiateViewControllerWithIdentifier:@"contactUs"]];
UIViewController *subController = [self.storyboard instantiateViewControllerWithIdentifier:@"contactUs"];
[mainView addSubview:subController.view];
}
}
我的错误来当我选择选择段2来查看的UITableViewController作为子视图 X代码给我以下错误
EXC_BAD_ACCESS(代码= 1个地址= 0x110ff210上利ne NSDictionary * latests = [latestArray objectAtIndex:indexPath.row];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"latestCell1";
latestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *latests = [latestArray objectAtIndex:indexPath.row];
NSString *latest_name_En = [latests objectForKey:@"title_en"];
NSString *logo1 = [latests objectForKey:@"logo"];
NSString *img1 = [latests objectForKey:@"img1"];
NSData *latestsImg1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:img1]];
NSData *latestsLogo1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:logo1]];
cell.latest_name_en1.text = latest_name_En;
dispatch_async(dispatch_get_main_queue(), ^{
cell.latest_img1.image = [UIImage imageWithData:latestsImg1];
cell.latest_logo1.image = [UIImage imageWithData:latestsLogo1];
});
return cell;
}
我敢肯定的UITableViewController工作正常,因为我试过单独运行它,它也努力尝试过没有自定义单元格,它也工作,但与custon细胞作为一个子视图它给上述错误。
latestArray尚未正确初始化。
任何时候当你获得EXEC_BAD_ACCESS时,通过僵尸工具运行你的代码。这将指向任何有问题的代码。
对象latestsImg1和latestsLogo1在初始化时未被保留。因此,当在dispatch_async中执行的异步代码块运行时,这些值可能已经被释放并且不再有效。所以,当异步模块执行这些指针时,这两个指针就会被忽略。我建议要么不通过GrandCentral分发来完成这两项任务,要么必须异步执行这两项任务,然后在创建之后保留它们,并在异步模块中释放它们。
是这样的:
的NSData * latestsImg1 = [NSData的dataWithContentsOfURL:[NSURL URLWithString:IMG1]]; [latestsImg1 retain]; NSData * latestsLogo1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:logo1]];
[latestsLogo1 retain]; cell.latest_name_en1.text = latest_name_En;
dispatch_async(dispatch_get_main_queue(), ^{
cell.latest_img1.image = [UIImage imageWithData:latestsImg1];
cell.latest_logo1.image = [UIImage imageWithData:latestsLogo1];
[latestsImg1 release];
[latestLogo1 release];
});
我试过但用保留给出了一个错误 – user1434025
感谢您的帮助
我终于弄明白我用
[self addChildViewController:subController];
[mainView addSubview:subController.view];
到tableview中添加到ViewController作为子视图
和它的工作
张贴任何与创建latestArray相关的代码,也请发布方法codeOfRowsInSection –