iphone - 在 uiviewcontroller 中添加 uitableviewcontroller 作为 subview 时出错

标签 iphone ios5 xcode4.3

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

我在一个 UIViewController 中使用 UISegment 来添加不同的 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];
    }
}

当我选择 select Segment 2 将 uitableviewcontroller 作为 subview 查看时出现错误 x 代码给我以下错误

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 工作正常,因为我尝试单独运行它,它也可以在没有自定义单元的情况下尝试它,它也可以工作,但使用自定义单元作为 subview 时,它会给出上面的错误。

最佳答案

感谢您的帮助

我终于弄清楚我用过

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

将 TableView 作为 subview 添加到 View Controller

成功了

关于iphone - 在 uiviewcontroller 中添加 uitableviewcontroller 作为 subview 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10874135/

相关文章:

iphone - 如何将文本从文本字段发送到另一个 View Controller

iphone - 如何在 alertview ios 中删除 %20

iphone - 从新的 fbconnect 登录 Facebook

macos - Lion 10.7.2 Xcode 4.3上的OS X应用程序::错误:无法启动<exe路径>-SBTarget无效

Xcode 4.3.2 和 iOS 5.1.1 + 越狱 : App installs but crash on launch

iphone - 代码 : How to change all fonts at once in an app?

ios - 在 iOS 5 上关闭 UIImagePickerController 后出现白屏?

iphone - 在具有导航 Controller 的 UIviewController 中添加 tabbarController 的最佳方法?

iphone - 以编程方式打开设置 iOS 6

iphone - 将 "Strip Debug Symbols During Copy"设置为NO后经常出现内存警告