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

当我选择选择段 2 以查看 uitableviewcontroller 作为子视图 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 工作正常,因为我试图单独运行它,它也可以在没有自定义单元格的情况下尝试它,它也可以工作,但是使用自定义单元格作为子视图它会给出上述错误。

4

3 回答 3

14

谢谢您的帮助

我终于弄清楚我用过

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

将 tableview 作为子视图添加到 viewcontroller

它奏效了

于 2012-06-04T08:27:35.343 回答
0

latestArray 未正确初始化。

任何时候你得到 EXEC_BAD_ACCESS,通过 Zombies Instrument 运行你的代码。这将指向任何有问题的代码。

于 2012-06-03T22:49:14.620 回答
0

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

就像是:

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

});
于 2012-06-04T01:18:06.330 回答