ios-调用ViewController时标签会重复

ios-调用ViewController时标签会重复,ios,uinavigationcontroller,custom-cell,Ios,Uinavigationcontroller,Custom Cell,我有一个带有CoreData和自定义单元格的TableView,所有这些都可以正常工作 现在,当我选择一个单元格编辑其内容并从中返回时,只需点击UINavigationController中的“后退”按钮,就会发生一些奇怪的事情 请看图片我的意思。似乎更新的UILabel是放在旧标签之上,而不是“更新”的?? 我错过什么了吗 您将在顶部单元格中看到差异,但所有单元格都会出现这种情况 编辑:添加代码 - (UITableViewCell *)tableView:(UITableView *)t

我有一个带有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;
 }

UITableView通过回收单元格来工作。当表视图从其委托请求单元格时,如果存在未使用的单元格,它将使用该单元格。否则,它将分配一个新的。只应在分配新单元格时添加标签。如果改为回收,则只需设置现有标签的文本


像这样:

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

我怀疑的是你的计划有问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法实现。你能把代码贴在这里吗

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


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

此内容需要在分配块之后移出:

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

当您为UILabel设置背景色时,此问题就解决了。我把它设置为:

[UIColor clearColor];
这意味着我会继续观察以前所有的参赛作品


因此,将其设置为任何颜色,例如
[UIColor blueColor]

是的,你是对的,我正在添加子视图-我已经粘贴了代码所有文本的设置都应该发生在if(customCell==nil){customCell=[[MNCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];}块之外。所有与设置布局、颜色等相关的事情都应该发生在该块中。这似乎不起作用,我得到的是空白单元格。addSubView需要在该块之外,作为回报,我还需要该块之外的alloc&init??alloc和init应该在该块之内。像MNotes*MNotes=[[self-fetchedResultsController]objectAtIndexPath:indexPath]这样的行//标题和摘要的字符串NSString*titleString=mnotes.noteTileString;NSString*summaryString=mnotes.mainNoteString;customCell.noteTitle.text=标题字符串;customCell.noteSummary.text=summaryString;所有这些都是块外的(返回之前),所以我正在做我认为你告诉我的事情-我已经更新了代码,但现在我的单元格是空的。请查看我编辑的答案。什么是_mainTableView?还可以在我的回答中看到新问题。\u mainTableView是UIViewController中的TableView-如果我使用普通的TableView,我总是会出现警告,因为它在该方法中被重新定义,因此,此更改消除了警告,从未引起任何问题。这是我们正在查看的UITableViewController子类吗?不是添加了TableView的UIViewController。我想我的问题是,无论我在您所指的代码块中放了什么,似乎没有在我需要的部分中使用。请参阅我的其他帖子。一旦它开始回收细胞,你将有不同的问题。