0

大家好,我有一个自定义tableview cells名称product cell,rating cell,notify cell,其中product cell包含textfield它使用的位置section 0,1,2,4。问题是如果我textfiledssection 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;
    }
4

1 回答 1

0

Your table view cells are being reused. This is the way table view cells are being optimized.

If you know exactly how many sections and rows the table view has and they will not change, consider using static table view cells which is also suitable for forms:

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

Look at the section The Technique for Static Row Content.

Otherwise you will need to save the value of each text field into a variable, such as rating, email, description, and put it back to the text field every time the respective table view cell is loaded.

于 2013-05-22T04:50:22.913 回答