在uiviewcontroller中将uitableviewcontroller添加为子视图时出错

时间:2012-06-03 21:38:54

标签: iphone ios5 xcode4.3

我正在做一个简单的应用程序来查看来自json服务器的项目

我在一个UIViewController中使用了UISegment来添加不同的子视图

    - (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];
    }
}

当我选择Segment 2以将uitableviewcontroller视为子视图时,出现错误 x代码给我以下错误

  

exc_bad_access(代码= 1地址= 0x110ff210在线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 cell作为子视图它会给出上面的错误。

3 个答案:

答案 0 :(得分:14)

感谢您的帮助

我终于搞清楚了我用过

[self addChildViewController:subController];
[mainView addSubview:subController.view];

将tableview作为子视图添加到viewcontroller

并且有效

答案 1 :(得分:0)

latestArray尚未正确初始化。

每当您获得EXC_BAD_ACCESS时,请通过Zombies Instrument运行您的代码。这将指向任何有问题的代码。

答案 2 :(得分:0)

对象latestsImg1和latestsLogo1在初始化时不会被保留。因此,当运行dispatch_async中执行的异步代码块时,这些值可能已经释放并且不再有效。因此,当异步块执行时,这两个指针被指向遗忘。我建议不要通过GrandCentral调度执行这两个赋值,或者如果必须异步执行它们,则在创建后保留它们并在异步块中释放它们。

类似的东西:

NSData * latestsImg1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:img1]]; [latestsImg1保留]; NSData * latestsLogo1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:logo1]];
[latestsLogo1保留]; 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];

});