0

私のアプリの UITableViewController は、json データ ソースからデータを取得します。また、CG を使用してカスタム UITableViewCell 背景を作成しました。発生する非常に興味深いバグがあり、その理由はわかりません。何が起こるか、どのように再現するかについて説明します。

タップしてテーブル ビューに入ります。テーブルをまったくスクロールせずに、ビュー内のアイテムをすぐにタップします。その項目をタップした後、戻るボタンを押してテーブル ビューに戻ります。次に、画面外から表示される最初のセルを下にスクロールすると、カスタム背景がありません。これは、セルのデフォルトになります。次に、10 番目のセルごとに下にスクロールし続けると、同じ問題が発生します。

このバグは、この正確なプロセスでのみ発生します。アイテムをタップする前にテーブルビューをスクロールしても、それは起こりません。

テーブルビュー コントローラーに関連するコードは次のとおりです。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Will remove all of the used codes from the table if setting is enabled
    if (self.shouldHideCodes) {
        NSMutableArray *tempArray = [self.jsonCodeData mutableCopy];
        [tempArray removeObjectsInArray:[self.usedCodes usedCodes]];
        self.jsonCodeData = tempArray;
    }

    return [self.jsonCodeData count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;

    if (self.jsonCodeData) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"code cell"];

        if ([cell isKindOfClass:[CodeCellTVC class]]) {
            CodeCellTVC *tvcCell = (CodeCellTVC *)cell;

            if (![tvcCell.backgroundView isKindOfClass:[CustomCellBackground class]]) {
                tvcCell.backgroundView = [[CustomCellBackground alloc] init];
            }

            NSDictionary *codeDict = [self.jsonCodeData objectAtIndex:indexPath.row];


            // Retrieve code string from dictionary
            NSString *codeText = [NSString stringWithFormat:@"%@", [codeDict objectForKey:@"code"]];

            tvcCell.codeTableLabel.text = codeText;

        }
    }

    return cell;
}

私を混乱させるのは、それがどのように反応するかです。バグが発生すると、すべてのセルではなく、10 番目のセルごとに問題が発生します。これらのメソッド以外に、tableviewcell 自体を処理するものはありません。

4

1 に答える 1

0

私はあなたの問題を理解しました。セルの初期化時に間違っていました。セルを初期化するたびに、そのセルにメモリが割り当てられるたびに、メモリの問題が発生します。以下のようにコードを編集してください。

    cell = [tableView dequeueReusableCellWithIdentifier:@"code cell"];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"code cell"];
    }
于 2014-05-17T11:13:20.210 に答える