滚动tableview时,textfield文本会在其他文本字段上留下印象

时间:2013-05-22 03:18:51

标签: objective-c uitableview uikit uitextfield

大家好,我有自定义tableview cells名为product cell,rating cell,notify cell,其中product cell包含textfield section 0,1,2,4中使用的textfileds。问题是如果我在section 0中的textfileds中输入文字。滚动时我在section1,2中的 -(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; } 上留下了文字印象。你们可以帮帮我。下面是代码

{{1}}

1 个答案:

答案 0 :(得分:0)

您的表格视图单元格正在重复使用。这是表格视图单元格优化的方式。

如果您确切知道表视图有多少个部分和行,并且它们不会更改,请考虑使用也适用于表单的静态表视图单元格:

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html

查看静态行内容技术部分。

否则,您需要将每个文本字段的值保存到变量中,例如评级,电子邮件,描述,并在每次加载相应的表格视图单元格时将其重新放回文本字段。