停止从原型单元格中的一个 UITextView 添加到另一个单元格的输入文本
Stop inputted text from one UITextView within a prototype cell adding to another
我有一个奇怪的问题,我无法深入了解。我有一个 UITableView,其中包含各种原型单元格,具体取决于存储在可变数组中的内容。它的工作原理是这样的:
if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutSubtitle class]]){
cellId =@"CellHandoutSubtitle";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
..
}
else if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutParagraph class]]){
cellId =@"CellHandoutGeneral";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
..
}
else if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutQuote class]]){
cellId =@"CellHandoutQuote";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
..
}
else if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutSimpleQuestion class]]){
cellId =@"CellHandoutSimpleQuestion";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
UILabel *questionLabel = (UILabel *)[cell viewWithTag:1];
questionLabel.text = questionText;
UITextView *theTextView = (UITextView *)[cell viewWithTag:2];
theTextView.tag = indexPath.row + 100;
theTextView.delegate = self;
..
}
问题是当我在 CellHandoutSimpleQuestion 单元格中向 UITextView 添加文本时,它会将用户输入重复到其下方的另一个单元格中,如下图所示,有人知道为什么吗?
发生这种情况是因为您正在重用单元格的 UITextView。为避免这种情况,您需要将 UITextView 的文本存储在模型的 属性 中,并将该文本设置在最后一位
...
else if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutSimpleQuestion class]]){
cellId =@"CellHandoutSimpleQuestion";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
UILabel *questionLabel = (UILabel *)[cell viewWithTag:1];
questionLabel.text = questionText;
UITextView *theTextView = (UITextView *)[cell viewWithTag:2];
theTextView.tag = indexPath.row + 100;
theTextView.delegate = self;
the.TextView.text = yourModelForTheCell.textViewText;
..
}
如果您需要支持编辑,则必须在委托方法 textView:shouldChangeTextInRange:replacementText:
上为对应的模型对象更新 属性
通常当您看到前一个单元格的剩余部分时,这是因为它正在被重复使用并且值没有被重置。如果我正确地遵循你的代码,它可能是这样的。
else if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutSimpleQuestion class]]){
cellId =@"CellHandoutSimpleQuestion";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
UILabel *questionLabel = (UILabel *)[cell viewWithTag:1];
questionLabel.text = questionText;
UITextView *theTextView = (UITextView *)[cell viewWithTag:2];
theTextView.tag = indexPath.row + 100;
theTextView.delegate = self;
theTextView.text = @"";
//or set text if there is text to set
..
}
我有一个奇怪的问题,我无法深入了解。我有一个 UITableView,其中包含各种原型单元格,具体取决于存储在可变数组中的内容。它的工作原理是这样的:
if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutSubtitle class]]){
cellId =@"CellHandoutSubtitle";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
..
}
else if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutParagraph class]]){
cellId =@"CellHandoutGeneral";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
..
}
else if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutQuote class]]){
cellId =@"CellHandoutQuote";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
..
}
else if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutSimpleQuestion class]]){
cellId =@"CellHandoutSimpleQuestion";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
UILabel *questionLabel = (UILabel *)[cell viewWithTag:1];
questionLabel.text = questionText;
UITextView *theTextView = (UITextView *)[cell viewWithTag:2];
theTextView.tag = indexPath.row + 100;
theTextView.delegate = self;
..
}
问题是当我在 CellHandoutSimpleQuestion 单元格中向 UITextView 添加文本时,它会将用户输入重复到其下方的另一个单元格中,如下图所示,有人知道为什么吗?
发生这种情况是因为您正在重用单元格的 UITextView。为避免这种情况,您需要将 UITextView 的文本存储在模型的 属性 中,并将该文本设置在最后一位
...
else if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutSimpleQuestion class]]){
cellId =@"CellHandoutSimpleQuestion";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
UILabel *questionLabel = (UILabel *)[cell viewWithTag:1];
questionLabel.text = questionText;
UITextView *theTextView = (UITextView *)[cell viewWithTag:2];
theTextView.tag = indexPath.row + 100;
theTextView.delegate = self;
the.TextView.text = yourModelForTheCell.textViewText;
..
}
如果您需要支持编辑,则必须在委托方法 textView:shouldChangeTextInRange:replacementText:
通常当您看到前一个单元格的剩余部分时,这是因为它正在被重复使用并且值没有被重置。如果我正确地遵循你的代码,它可能是这样的。
else if([currentHandout.content[indexPath.row - 3] isKindOfClass: [objHandoutSimpleQuestion class]]){
cellId =@"CellHandoutSimpleQuestion";
cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
UILabel *questionLabel = (UILabel *)[cell viewWithTag:1];
questionLabel.text = questionText;
UITextView *theTextView = (UITextView *)[cell viewWithTag:2];
theTextView.tag = indexPath.row + 100;
theTextView.delegate = self;
theTextView.text = @"";
//or set text if there is text to set
..
}