Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将单元格文本重新打印到UITableViewCell中_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 将单元格文本重新打印到UITableViewCell中

Ios 将单元格文本重新打印到UITableViewCell中,ios,objective-c,uitableview,Ios,Objective C,Uitableview,进入UITableViewController类的cellForRowAtIndexPath:方法,我正在使用下一个代码显示包含UITextView的单元格 有时,当我滚动包含单元格的表格,然后滚动单元格UITextView,它会显示重新打印的单元格文本(好像有两个UITextView对象,而不是一个,进入单元格) 我能做些什么来解决这个问题 static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableVie

进入
UITableViewController
类的
cellForRowAtIndexPath:
方法,我正在使用下一个代码显示包含
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重用其单元格以提高滚动性能。每当重复使用单元格时,您都会向该单元格添加一个新的文本视图(尽管其中已有一个)


您应该将文本视图的创建(并将其添加到单元格)移动到
if(cell==nil)
块中。在该块内部,还为文本视图提供一个唯一的
标记
,并使用该
标记
属性从块外部访问文本视图。有关此模式的示例,请参阅Apple的table view示例代码。UITableView已被大量使用。

UITableView重用其单元格以提高滚动性能。每当重复使用单元格时,您都会向该单元格添加一个新的文本视图(尽管其中已有一个)

您应该将文本视图的创建(并将其添加到单元格)移动到
if(cell==nil)
块中。在该块内部,还为文本视图提供一个唯一的
标记
,并使用该
标记
属性从块外部访问文本视图。有关此模式的示例,请参见Apple的table view示例代码,它被大量使用