2

だから、私は奇妙な問題を抱えています。私は Parse の iOS API を使用しており、PFQueryTableViewController を使用してアイテムをユーザーに表示しています。これらの項目は 2 つのセクションに分かれています。アプリケーションが最初に起動すると、objectsDidLoadメソッドを実行する前にメソッドが実行されnumberOfRowsInSectionます。これは完璧ですobjectsDidLoadメソッドは、テーブル ビューの各セクションに存在するオブジェクトの数を定義できます。また、完全に機能しているtableViewの「プルダウンして更新」機能を有効にしています。そのため、解析データ ブラウザで特定のデータ値を変更すると、ユーザーに表示されるアイテムの 1 つがセクションを変更するはずです。そのデータ値を変更した後にアプリを再起動すると、オブジェクトは他のセクションに表示されます。ただし、問題は、解析時にデータ値を変更してオブジェクトのセクションを変更し、tableView をプルダウンしてデータを更新すると、アプリがクラッシュし、その理由はわかっていると思います。このメソッドは、アプリの初回objectsDidLoad実行時にメソッドの前に実行されますが、実際にはアプリの実行後に実行されます。numberOfRowsInSectionnumberOfRowsInSectiontableView をプルダウンしてデータを更新すると。これにより、cellForRowAtIndexPathメソッドは、各セクションにオブジェクトを格納する配列に存在しない配列インデックスにアクセスしようとします。

numberOfRowsInSection私の質問:メソッドが呼び出される前に、オブジェクトを更新し、各セクションのオブジェクトを格納する配列を更新するにはどうすればよいですか?

これが私のobjectsDidLoad方法です:

    - (void)objectsDidLoad:(NSError *)error {
       //dataArray, firstSectionArray, and secondSectionArray are global NSMutableArrays
       [super objectsDidLoad:error];

       firstSectionArray = [[NSMutableArray alloc]init];
       secondSectionArray = [[NSMutableArray alloc]init];
       NSMutableDictionary *firstSectionDictionary = [[NSMutableDictionary alloc]init];
       NSMutableDictionary *secondSectionDictionary = [[NSMutableDictionary alloc]init];
       dataArray = [[NSMutableArray alloc]init];

       for (int i = 0; i < [self.objects count]; i++) {
           if ([[[self.objects objectAtIndex:i]objectForKey@"Level"]intValue] == 1) {
               [firstSectionArray addObject:[self.objects objectAtIndex:i]];
           }
           else {
               [secondSectionArray addObject:[self.objects objectAtIndex:i]];
           }
       }

       firstSectionDictionary = [NSMutableDictionary dictionaryWithObject:firstSectionArray forKey:@"data"];
       secondSectionDictionary = [NSMutableDictionary dictionaryWithObject:secondSectionArray forKey:@"data"];
       [dataArray addObject:firstSectionDictionary];
       [dataArray addObject:secondSectionDictionary];
    }

これが私のnumberOfRowsInSection方法です:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        NSDictionary *dictionary = [dataArray objectAtIndex:section];
        NSMutableArray *array = [dictionary objectForKey:@"data"];
        return [array count];
    }

これが私のcellForRowAtIndexPath方法です:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
        static NSString *CellIdentifier = @"Cell";

        PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

        NSMutableDictionary *dictionary = [dataArray objectAtIndex:indexPath.section];
        //This next line is where it's crashing
        PFObject *newObject = [[dictionary objectForKey:@"data"]objectAtIndex:indexPath.row];

        //More irrelevant code below..........
    }
4

2 に答える 2