gpt4 book ai didi

ios - 将单元格文本重新打印到 UITableViewCell

转载 作者:行者123 更新时间:2023-12-01 18:03:18 25 4
gpt4 key购买 nike

进入cellForRowAtIndexPath: UITableViewController 的方法类我正在使用下一个代码来显示一个包含 UITextView 的单元格.

有时当我滚动包含单元格的表格,然后滚动单元格 UITextView ,它显示重新打印的单元格文本(好像有两个 UITextView 对象,而不是一个,进入单元格)。

我能做些什么来解决这个问题?

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

cell.contentView.bounds = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
cell.selectionStyle = UITableViewCellSelectionStyleNone;

UITextView *textView = [[UITextView alloc] initWithFrame:cell.contentView.bounds];
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
textView.editable = NO;
textView.scrollEnabled = YES;
textView.backgroundColor = [UIColor clearColor];
textView.font = [UIFont fontWithName:@"Helvetica" size:14.0];
textView.text = self.description;

[cell.contentView addSubview:textView];
[textView release];

最佳答案

UITableView 重用其单元格来提高滚动性能。每当重用一个单元格时,您都会向该单元格添加一个新的 TextView (尽管已经存在一个)。

您应该将 TextView 的创建(并将其添加到单元格)移动到 if (cell == nil)堵塞。在这个 block 内,也给 TextView 一个唯一的tag并使用这个tag属性以从 block 外部访问 TextView 。有关此模式的示例,请参阅 Apple 的 table view 示例代码,它被大量使用。

关于ios - 将单元格文本重新打印到 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4185013/

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