ios - 调用ViewController时标签会重复

时间:2012-12-02 20:23:43

标签: ios uinavigationcontroller custom-cell

我有一个带有CoreData和Custom Cells的TableView,一切正常。

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

请看图像我的意思。似乎更新的UILabels被置于旧的UILabels而不是'更新'? 我错过了什么吗?

您将看到顶部单元格中的差异,但它会发生在所有这些区域中。

Before

After

编辑:添加代码

- (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 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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];

答案 2 :(得分:0)

我怀疑你的

有什么问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

方法实现。你能在这里粘贴代码吗?

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

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

答案 3 :(得分:0)

当您为UILabel设置背景颜色时,此问题已得到解决。我把它设置为:

[UIColor clearColor];

这意味着我会继续看到以前所有参赛作品的内容。

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