滚动表格视图时,文本字段文本在其他文本字段上留下印象
Posted
技术标签:
【中文标题】滚动表格视图时,文本字段文本在其他文本字段上留下印象【英文标题】:textfield text leaves an impression on other textfiled when tableview is scrolled 【发布时间】:2013-05-22 03:18:51 【问题描述】:大家好,我有自定义 tableview
cells
称为 product cell,rating cell,notify cell
,其中 product cell
包含 textfield
,它在 section 0,1,2,4
中使用。问题是如果我在section 0
中的textfileds
中输入文本。滚动时,我在section1,2
中的textfileds
上留下了文字印象。你们能帮帮我吗。下面是代码
-(ProductCell *)getProductCell
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ProductCell" owner:nil options:nil];
ProductCell *cell;
for (id currentObject in topLevelObjects)
if ([currentObject isKindOfClass:[ProductCell class]])
cell= (ProductCell*)currentObject;
return cell;
return nil;
-(RatingCell *)getRatingCell
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RatingCell" owner:nil options:nil];
RatingCell *cell;
for (id currentObject in topLevelObjects)
if ([currentObject isKindOfClass:[RatingCell class]])
cell= (RatingCell*)currentObject;
return cell;
return nil;
-(NotifyCell *)getNotifyCell
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NotifyCell" owner:nil options:nil];
NotifyCell *cell;
for (id currentObject in topLevelObjects)
if ([currentObject isKindOfClass:[NotifyCell class]])
cell= (NotifyCell *)currentObject;
return cell;
return nil;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";
static NSString *CellIdentifier3 = @"Cell3";
ProductCell *productCell;
NotifyCell *notifyCell;
productCell=(ProductCell *)[tblAddProducts dequeueReusableCellWithIdentifier:CellIdentifier1];
notifyCell = (NotifyCell*)[tblAddProducts dequeueReusableCellWithIdentifier:CellIdentifier2];
UINib * nib1 = [UINib nibWithNibName:@"ProductCell" bundle: nil];
[tableView registerNib:nib1 forCellReuseIdentifier:CellIdentifier1];
UINib * nib2 = [UINib nibWithNibName:@"NotifyCell" bundle: nil];
[tableView registerNib:nib2 forCellReuseIdentifier:CellIdentifier2];
UINib * nib3 = [UINib nibWithNibName:@"RatingCell" bundle: nil];
[tableView registerNib:nib3 forCellReuseIdentifier:CellIdentifier3];
if (notifyCell == nil)
notifyCell = [self getNotifyCell];
if (ratingCell == nil)
ratingCell = [self getRatingCell];
if (productCell == nil)
productCell = [self getProductCell];
productCell.txtField.delegate=self;
productCell.selectionStyle = UITableViewCellSelectionStyleNone;
notifyCell.selectionStyle = UITableViewCellSelectionStyleNone;
ratingCell.selectionStyle = UITableViewCellSelectionStyleNone;
switch (indexPath.section)
case 0:
productCell.lblName.text=[productTitleArray objectAtIndex:indexPath.row];
productCell.txtField.tag=indexPath.row;
if([productCell.lblName.text isEqualToString:@"Valid till"]) productCell.txtField.returnKeyType = UIReturnKeyDone;
else productCell.txtField.returnKeyType = UIReturnKeyNext;
cellSection = indexPath.section;
return productCell;
break;
case 1:
productCell.lblName.text=[invoiceTitleArray objectAtIndex:indexPath.row];
productCell.txtField.tag=indexPath.row;
if ([productCell.lblName.text isEqualToString:@"Bank Name"]) productCell.txtField.returnKeyType = UIReturnKeyDone;
else productCell.txtField.returnKeyType = UIReturnKeyNext;
cellSection = indexPath.section;
return productCell;
break;
case 2:
productCell.lblName.text=[warrantyTitleArray objectAtIndex:indexPath.row];
productCell.txtField.tag=indexPath.row;
if ([productCell.lblName.text isEqualToString:@"Valid till"]) productCell.txtField.returnKeyType = UIReturnKeyDone;
else productCell.txtField.returnKeyType = UIReturnKeyNext;
cellSection = indexPath.section;
return productCell;
break;
case 3:
productCell.lblName.text=@"Description";
productCell.txtField.tag=indexPath.row;
if ([productCell.lblName.text isEqualToString:@"Description"]) productCell.txtField.returnKeyType = UIReturnKeyDone;
cellSection = indexPath.section;
return productCell;
break;
case 4:
ratingCell.lblName.text=@"Rating";
ratingCell.starRatingControl.delegate=self;
cellSection = indexPath.section;
return ratingCell;
break;
case 5:
productCell.lblName.text=[serviceContactsTitleArray objectAtIndex:indexPath.row];
productCell.txtField.tag=indexPath.row;
if ([productCell.lblName.text isEqualToString:@"Email"]) productCell.txtField.returnKeyType = UIReturnKeyDone;
else productCell.txtField.returnKeyType = UIReturnKeyNext;
cellSection = indexPath.section;
return productCell;
break;
case 6:
notifyCell.lblName.text=[notifyTitleArray objectAtIndex:indexPath.row];
cellSection = indexPath.section;
return notifyCell;
break;
default:
break;
productCell.selectionStyle = UITableViewCellSelectionStyleNone;
return productCell;
【问题讨论】:
要么您将所有UITextfields
连接到同一个IBOutlet
..
是的......如果你想我反刍给你整个代码..
不,我不需要完整的代码。你可以自己检查一下你是如何连接 IBOutlets 的 tableViewCell
我在我的自定义单元格中有一个名为产品单元格的文本字段,它在 tableview 的不同部分中重复使用,并且只有一个连接
由于dequeueReusableCellWithIdentifier方法,文本字段可能被表格视图重用。
【参考方案1】:
您的表格视图单元格正在被重复使用。这就是优化表格视图单元格的方式。
如果您确切知道表格视图有多少个部分和行并且它们不会更改,请考虑使用也适用于表格的静态表格视图单元格:
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html
查看静态行内容技术部分。
否则,您需要将每个文本字段的值保存到一个变量中,例如评级、电子邮件、描述,并在每次加载相应的表格视图单元格时将其放回文本字段。
【讨论】:
如果我给你完整的源代码你能帮我解决dis ...因为我使用多个自定义单元格...以上是关于滚动表格视图时,文本字段文本在其他文本字段上留下印象的主要内容,如果未能解决你的问题,请参考以下文章