gpt4 book ai didi

ios - 调用 ViewController 时标签会重复

转载 作者:行者123 更新时间:2023-11-28 22:44:15 25 4
gpt4 key购买 nike

我有一个带有 CoreData 和自定义单元格的 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;
}

最佳答案

UITableViews 通过回收单元格来工作。当 TableView 从它的委托(delegate)请求一个单元格时,如果有一个现有的未使用的单元格,它将使用它。否则它会分配一个新的。您应该只在分配新单元格时添加标签。如果它被回收,您只想设置现有标签的文本。


像这样:

    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;

注意:实例化时,单元格的内容 View 和大小可能为 0,0。它可以在给定实际大小时调整 subview 的大小。


这看起来很奇怪...为什么不使用调用委托(delegate)的 tableView?像平常一样:

customCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

关于ios - 调用 ViewController 时标签会重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13673467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com