0

我有一个带有 CoreData 和自定义单元格的 TableView,它们都可以正常工作。

现在,当我选择一个单元格以编辑其内容并从中返回时,只需点击 UINavigationController 中的后退按钮,我就会发生一些奇怪的事情。

请看图片我的意思。似乎更新后的 UILabel 被放置在旧 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 回答
1

这些东西需要在分配块之后移出。:

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

这些东西真的应该放在你的 UITableViewCell 子类定义中,但它现在可以保留:

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.contentView addSubview:customCell.noteTitle];
[customCell.contentView addSubview:customCell.noteSummary];
[customCell.contentView addSubview:customCell.noteDate];
于 2012-12-02T22:27:43.550 回答
0

我怀疑是你的有问题

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

方法实现。你能把代码贴在这里。

从描述中可以明显看出,您在回收单元后正在执行 [cell addSubview:label] 。因此,每次重新加载表格的单元格时,您不仅要设置标签的文本,还要添加一个子视图,然后对其进行设置。

你应该做的是从 [tableView dequeueReusableCellWithIdentifier:@"identifier"] 中获取单元格,并且只有当单元格为 nil 时,才执行 addSubview。否则,只需设置子视图的文本

于 2012-12-02T20:40:47.653 回答
0

当你为你的 UILabel 设置背景颜色时,这个问题就解决了。我把它设置为:

[UIColor clearColor];

这意味着我将继续看到所有以前的条目发生了什么。

所以将它设置为任何颜色,例如[UIColor blueColor];

于 2012-12-02T22:31:42.937 回答