在uiviewcontroller中将uitableviewcontroller添加为子视图时出错 [英] error when add uitableviewcontroller as subview in uiviewcontroller

查看:122
本文介绍了在uiviewcontroller中将uitableviewcontroller添加为子视图时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm doing a simple app that views items from a json server

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

I used UISegment in one UIViewController to add different subview

    - (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代码给我以下错误

my error comes when I select select Segment 2 to view the uitableviewcontroller as subview x code give me the following error


exc_bad_access(代码= 1地址= 0x110ff210在线NSDictionary * latests = [latestArray objectAtIndex:indexPath.row];

exc_bad_access (code=1 address=0x110ff210 on line 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单元格作为子视图,它给出了上面的错误。

I'm sure the UITableViewController is working fine as I tried to run it alone and it work also tried it without custom cell and it worked also but with custon cell as a subview it gives the error above.

推荐答案

感谢您的帮助

我终于明白了我的意思used

I finally figure it out I used

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

将tableview作为子视图添加到viewcontroller

to add the tableview to the viewcontroller as subview

它起作用了

这篇关于在uiviewcontroller中将uitableviewcontroller添加为子视图时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆