3

私はjsonサーバーからアイテムを表示する簡単なアプリをやっています

1 つの 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];
    }
}

select Segment 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];

テーブルビューをビューコントローラーにサブビューとして追加する

そしてそれは働いた

于 2012-06-04T08:27:35.343 に答える
0

オブジェクトlatestsImg1およびlatestsLogo1は、初期化時に保持されません。したがって、dispatch_async内で実行される非同期コードブロックが実行されると、それらの値はおそらくすでに解放されており、無効になっています。したがって、非同期ブロックが実行されると、これら2つのポインターは忘却に向けられます。GrandCentralディスパッチを介してこれらの2つの割り当てを行わないか、非同期で行う必要がある場合は、作成後にそれらを保持し、非同期ブロック内で解放することをお勧めします。

何かのようなもの:

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

});
于 2012-06-04T01:18:06.330 に答える
0

latestArray が正しく初期化されていません。

EXEC_BAD_ACCESS を取得するたびに、Zombies Instrument を介してコードを実行します。これにより、問題のあるコードが示されます。

于 2012-06-03T22:49:14.620 に答える