0

CoreDataとカスタムセルを備えたTableViewがあり、すべて正常に機能します。

UINavigationControllerの戻るボタンをタップするだけで、コンテンツを編集してそこから戻るセルを選択すると、何か奇妙なことが起こります。

私が言っている画像を見てください。更新されたUILabelは、「更新」されるのではなく、古いものの上に配置されているようです。私は何か見落としてますか??

一番上のセルに違いがありますが、それはすべてのセルに起こります。

前

後

編集:コードを追加

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

customCell = [_mainTableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (customCell == nil)
{
    customCell = [[MNCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    CGRect frameTitle;
    frameTitle = CGRectMake(20, 4, 195, 21);

    CGRect frameSummary;
    frameSummary = CGRectMake(20, 25, 250, 30);

    CGRect frameDate;
    frameDate = CGRectMake(200, 4, 100, 21);

    MNotes *mnotes = [[self fetchedResultsController] objectAtIndexPath:indexPath];

    //Strings for Title and Summary
    titleString = mnotes.noteTitleString;
    summaryString = mnotes.mainNoteString;

    NSLog(@"TITLE STRING = %@", titleString);

    //Date
    SORelativeDateTransformer *relativeDateTransformer = [[SORelativeDateTransformer alloc] init];
    relativeDate = [relativeDateTransformer transformedValue:mnotes.createDate];

    customCell.noteTitle = [[UILabel alloc] initWithFrame:frameTitle];
    customCell.noteTitle.backgroundColor = [UIColor clearColor];
    customCell.noteTitle.font = [UIFont systemFontOfSize:20];
    customCell.noteTitle.userInteractionEnabled = NO;
    customCell.noteTitle.textColor = [self colorWithHexString:@"274D70"];

    customCell.noteSummary = [[UITextView alloc] initWithFrame:frameSummary];
    customCell.noteSummary.backgroundColor = [UIColor clearColor];
    customCell.noteSummary.font = [UIFont systemFontOfSize:10];
    customCell.noteSummary.userInteractionEnabled = NO;

    customCell.noteDate = [[UILabel alloc] initWithFrame:frameDate];
    customCell.noteDate.backgroundColor = [UIColor clearColor];
    customCell.noteDate.font = [UIFont systemFontOfSize:16];
    customCell.noteDate.userInteractionEnabled = NO;
    customCell.noteDate.textColor = [self colorWithHexString:@"274D70"]; //#274D70

}

customCell.noteTitle.text = titleString;
customCell.noteSummary.text = summaryString;
customCell.noteDate.text = relativeDate;

[customCell.contentView addSubview:customCell.noteTitle];
[customCell.contentView addSubview:customCell.noteSummary];
[customCell.contentView addSubview:customCell.noteDate];

 return customCell;
 }
4

4 に答える 4

1

UITableViewsは、セルをリサイクルすることで機能します。テーブルビューがデリゲートからセルを要求するときに、既存の未使用のセルがある場合は、それを使用します。それ以外の場合は、新しいものを割り当てます。新しいセルが割り当てられたときにのみラベルを追加する必要があります。代わりにリサイクルする場合は、既存のラベルのテキストのみを設定する必要があります。


このような:

    customCell.noteDate.textColor = [self colorWithHexString:@"274D70"]; //#274D70

    [customCell.contentView addSubview:customCell.noteTitle];
    [customCell.contentView addSubview:customCell.noteSummary];
    [customCell.contentView addSubview:customCell.noteDate];
    // try adding this:
    customCell.contentView.autoresizesSubviews = false;
}

customCell.noteTitle.text = titleString;
customCell.noteSummary.text = summaryString;
customCell.noteDate.text = relativeDate;

注:インスタンス化されると、セルのコンテンツビューが表示され、サイズが0,0になる場合があります。実際のサイズが指定されている場合は、サブビューのサイズを変更している可能性があります。


これは奇妙に思えました...デリゲートを呼び出しているtableViewを使用しないのはなぜですか?通常のように:

customCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
于 2012-12-02T20:40:36.427 に答える
0

この問題は、UILabelに背景色を設定すると解決されます。私はそれを次のように設定しました:

[UIColor clearColor];

これは、以前のすべてのエントリに何が起こったのかを見続けることを意味しました。

したがって、任意の色に設定します。[UIColor blueColor];

于 2012-12-02T22:31:42.937 に答える
0

私が疑うのは、あなたに何か問題があるということです

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

メソッドの実装。ここにコードを貼り付けてもらえますか。

説明から明らかなことは、セルをリサイクルした後に[cell addSubview:label]を実行しているということです。したがって、テーブルのセルが再ロードされるたびに、ラベルのテキストを設定するだけでなく、サブビューを追加してから設定します。

[tableView dequeueReusableCellWithIdentifier:@ "identifier"]からセルを取得し、セルがnilの場合にのみ、addSubviewを実行する必要があります。それ以外の場合は、サブビューのテキストを設定するだけです

于 2012-12-02T20:40:47.653 に答える