表视图单元格在重新加载表视图和滚动时重复内容

Posted

技术标签:

【中文标题】表视图单元格在重新加载表视图和滚动时重复内容【英文标题】:Tableview cell Repeating the Content on reload tableview and scrolling 【发布时间】:2013-12-26 12:31:42 【问题描述】:

我的表格视图单元格内容在滚动和重新加载数据时重复。

我也知道将重用标识符更改为 nill 可以解决问题,但由于此链接ios 7 UITextField resignFirstResponder BAD中提到的原因,我需要使用重用标识符

有没有其他方法可以解决重复问题(以及重复使用单元格)

这是我的代码

代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    

    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
    cell.textLabel.textColor = [UIColor myPlaceholderColor];


    switch (indexPath.section) 
        case 0:

            if (indexPath.row == 0) 
                static NSString *ProfileTableIdentifier = @"ProfileCell";

                ProfileCell *profilecell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:ProfileTableIdentifier];
                if (profilecell == nil)
                
                    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProfileCell" owner:self options:nil];
                    profilecell = [nib objectAtIndex:0];
                

                profilecell.photoText.text = @"Photo";
                profilecell.photoText.hidden = YES;

                profilecell.seperatorView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableMiddleBackground.png"]];
                profilecell.leftImage1.image = [UIImage imageNamed:@"single tag.png"];
                profilecell.leftImage2.image = [UIImage imageNamed:@"tag.png"];

                profilecell.profileName.placeholder = @"Profile Name";
                profilecell.profileName.tag = 1;
                profilecell.profileName.autocapitalizationType = UITextAutocapitalizationTypeNone;

                profilecell.userName.placeholder = @"Username";
                profilecell.userName.tag = 2;
                profilecell.userName.autocapitalizationType = UITextAutocapitalizationTypeNone;

                profilecell.avatarImageView.layer.masksToBounds = YES;
                profilecell.avatarImageView.layer.cornerRadius = 6.0f;
                [profilecell.imageViewTap addTarget:self action:@selector(takePicture:)];

                if ([profileName length] != 0 ) 
                    profilecell.profileName.text = profileName;
                
                if ([userName length] != 0 ) 
                    profilecell.userName.text = userName;
                

                if (imageSelected)
                    profilecell.avatarImageView.image = avatarImage;
                else if ([profileImageUrl length] != 0 ) 
                    profilecell.avatarImageView.imageURL = [NSURL URLWithString:profileImageUrl];
                else 
                    profilecell.avatarImageView.image = [UIImage imageNamed:@"adduser.png"];
                

                return profilecell;
            else
                UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
                customImage.image =[UIImage imageNamed:@"profile bio.png"];
                customImage.contentMode = UIViewContentModeCenter;
                [cell.contentView addSubview:customImage];

                profileBioField = [[MyTextView alloc]initWithFrame:CGRectMake(40, 5, 250, MAX(35, profileBioRowHeight))];
                profileBioField.placeholder = @"Profile Bio";
                profileBioField.backgroundColor = [UIColor clearColor];
                profileBioField.font = [UIFont boldSystemFontOfSize:14];
                profileBioField.delegate = self;
                profileBioField.scrollEnabled = NO;
                //profileBioField.editable = NO;
                profileBioField.dataDetectorTypes = UIDataDetectorTypeAll;
                profileBioField.autocapitalizationType = UITextAutocapitalizationTypeNone;
                profileBioField.keyboardType = UIKeyboardTypeASCIICapable;
                [cell.contentView addSubview:profileBioField];

                if ([profileBio length] != 0 ) 
                    profileBioField.text = profileBio;
                

                return cell;
            

        
            break;
        case 1:


            if ([fbName length] ==0) 
                cell.textLabel.text = @"Use Your account info";
                cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow.png"]];
            else
                cell.textLabel.text = [NSString stringWithFormat:@"Logged in as %@",name];
                cell.accessoryView = nil;
            

            cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
            cell.textLabel.textColor = [UIColor myPlaceholderColor];


            return cell;

        break;
        case 2:

            if (indexPath.row == 0) 

                static NSString *CellIdentifier = @"GenderCell";

                UITableViewCell *gendercell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (gendercell == nil) 

                    gendercell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
                
                gendercell.selectionStyle = UITableViewCellSelectionStyleNone;
                male = [UIButton buttonWithType:UIButtonTypeCustom];
                male.frame = CGRectMake(10, 2, 45, 40);
                [male setBackgroundImage:[UIImage imageNamed:@"men in gray.png"] forState:UIControlStateNormal];
                [male setBackgroundImage:[UIImage imageNamed:@"men in blue.png"] forState:UIControlStateSelected];
                [male addTarget:self action:@selector(maleClicked:) forControlEvents:UIControlEventTouchUpInside];
                [gendercell.contentView addSubview:male];

                female = [UIButton buttonWithType:UIButtonTypeCustom];
                female.frame = CGRectMake(65, 2, 45, 40);
                [female setBackgroundImage:[UIImage imageNamed:@"women in gray.png"] forState:UIControlStateNormal];
                [female setBackgroundImage:[UIImage imageNamed:@"women in blue.png"] forState:UIControlStateSelected];
                [female addTarget:self action:@selector(femaleClicked:) forControlEvents:UIControlEventTouchUpInside];
                [gendercell.contentView addSubview:female];

                if ([gender isEqualToString: @"male"]) 
                    [male setSelected:YES];
                else if ([gender isEqualToString: @"female"])
                    [female setSelected:YES];
                
                return gendercell;

            else if(indexPath.row == 1) 
                UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
                customImage.image =[UIImage imageNamed:@"phone.png"];
                customImage.contentMode = UIViewContentModeCenter;
                [cell.contentView addSubview:customImage];


                phoneNumberField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
                phoneNumberField.tag = 3;
                phoneNumberField.font = [UIFont boldSystemFontOfSize:14];
                phoneNumberField.delegate = self;
                [cell.contentView addSubview:phoneNumberField];
                phoneNumberField.placeholder = @"Phone Number";
                phoneNumberField.keyboardType = UIKeyboardTypePhonePad;
                phoneNumberField.textColor = [UIColor darkGrayColor];
                phoneNumberField.clearButtonMode = UITextFieldViewModeWhileEditing;
                if ([phoneNumber length] != 0) 
                    phoneNumberField.text = phoneNumber;
                


             else if(indexPath.row == 2) 
                UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
                customImage.image =[UIImage imageNamed:@"mail box icon.png"];
                customImage.contentMode = UIViewContentModeCenter;
                [cell.contentView addSubview:customImage];


                emailIdField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
                emailIdField.tag = 4;
                emailIdField.font = [UIFont boldSystemFontOfSize:14];
                emailIdField.delegate = self;
                [cell.contentView addSubview:emailIdField];
                emailIdField.placeholder = @"Email Address";
                emailIdField.keyboardType = UIKeyboardTypeEmailAddress;
                emailIdField.autocapitalizationType = UITextAutocapitalizationTypeNone;
                emailIdField.textColor = [UIColor darkGrayColor];
                emailIdField.clearButtonMode = UITextFieldViewModeWhileEditing;
                emailIdField.autocorrectionType = UITextAutocorrectionTypeNo;

                if ([emailId length] != 0) 
                    emailIdField.text = emailId;
                


             else if(indexPath.row == 3) 

                UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
                customImage.image =[UIImage imageNamed:@"location.png"];
                customImage.contentMode = UIViewContentModeCenter;
                [cell.contentView addSubview:customImage];


                locationField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
                locationField.tag = 5;
                locationField.font = [UIFont boldSystemFontOfSize:14];
                locationField.delegate = self;
                [cell.contentView addSubview:locationField];
                locationField.placeholder = @"Location";
                locationField.textColor = [UIColor darkGrayColor];
                locationField.clearButtonMode = UITextFieldViewModeWhileEditing;
                if ([location length] != 0) 
                    locationField.text = location;
                

             else if(indexPath.row == 4) 
                UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
                customImage.image =[UIImage imageNamed:@"education.png"];
                customImage.contentMode = UIViewContentModeCenter;
                [cell.contentView addSubview:customImage];


                educationField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
                educationField.tag = 6;
                educationField.font = [UIFont boldSystemFontOfSize:14];
                educationField.delegate = self;
                [cell.contentView addSubview:educationField];
                educationField.placeholder = @"Education";
                educationField.textColor = [UIColor darkGrayColor];
                educationField.clearButtonMode = UITextFieldViewModeWhileEditing;

                if ([educationtxt length] != 0) 
                    educationField.text = educationtxt;
                

             else if(indexPath.row == 5) 

                UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
                customImage.image =[UIImage imageNamed:@"birthday.png"];
                customImage.contentMode = UIViewContentModeCenter;
                [cell.contentView addSubview:customImage];


                birthdayField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
                birthdayField.tag = 7;
                birthdayField.font = [UIFont boldSystemFontOfSize:14];
                birthdayField.delegate = self;
                [cell.contentView addSubview:birthdayField];
                birthdayField.placeholder = @"Birthday";
                birthdayField.textColor = [UIColor darkGrayColor];
                birthdayField.clearButtonMode = UITextFieldViewModeWhileEditing;

                if ([birthDay length] != 0) 
                    birthdayField.text = birthDay;
                

            

        
            break;

        case 3:

            if (indexPath.row == 0) 

                UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
                customImage.image =[UIImage imageNamed:@"keynew.png"];
                customImage.contentMode = UIViewContentModeCenter;
                [cell.contentView addSubview:customImage];


                passwordField = [[MyTextField alloc]initWithFrame:CGRectMake(50, 7, 250, 30)];
                passwordField.tag = 8;
                passwordField.font = [UIFont boldSystemFontOfSize:14];
                passwordField.delegate = self;
                [cell.contentView addSubview:passwordField];
                passwordField.placeholder = @"Create Password";
                passwordField.keyboardType = UIKeyboardTypeASCIICapable;
                passwordField.textColor = [UIColor darkGrayColor];
                passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
                passwordField.secureTextEntry = YES;

                if ([password length] != 0 ) 
                    passwordField.text = password;
                

             else 


            
        break;

        case 4:

            UIImageView *customImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44,44)];
            customImage.image =[UIImage imageNamed:@"camera.png"];
            customImage.contentMode = UIViewContentModeCenter;
            //[cell.contentView addSubview:customImage];

            cell.imageView.image = [UIImage imageNamed:@"camera.png"];
            cell.textLabel.text = @"Enable Camera";

            TTSwitch *squareThumbSwitch = [[TTSwitch alloc] initWithFrame:(CGRect) CGPointZero,  76.0f, 27.0f  ];
            squareThumbSwitch.trackImage = [UIImage imageNamed:@"square-switch-track"];
            squareThumbSwitch.overlayImage = [UIImage imageNamed:@"square-switch-overlay"];
            squareThumbSwitch.thumbImage = [UIImage imageNamed:@"square-switch-thumb"];
            squareThumbSwitch.thumbHighlightImage = [UIImage imageNamed:@"square-switch-thumb-highlight"];
            squareThumbSwitch.trackMaskImage = [UIImage imageNamed:@"square-switch-mask"];
            squareThumbSwitch.thumbMaskImage = nil; // Set this to nil to override the UIAppearance setting
            squareThumbSwitch.thumbInsetX = -3.0f;
            squareThumbSwitch.thumbOffsetY = -3.0f; // Set this to -3 to compensate for shadow

            [squareThumbSwitch setOn:[UserDefaults privacyStatus]];

            [squareThumbSwitch addTarget:self action:@selector(cameraChanged:) forControlEvents:UIControlEventValueChanged];
            cell.accessoryView = squareThumbSwitch;


            return cell;

        break;

        default:
            break;
    
    return cell;


【问题讨论】:

能否贴出cellForRowAtIndex的代码(表格视图的数据源方法) 我的意思是堆栈跟踪之类的东西,或者如果它在您的代码中,可能会指出该行。 您正在向单元格添加子视图。然后,当单元格被重用时,它仍然有这些自定义子视图,所以你会看到重复的内容。您可以通过使用viewWithTag: 查找和修改子视图来解决此问题,但我建议为每种类型的数据创建不同的 UITableViewCell 子类。 @AaronBrager 谢谢你的建议,我会试试看。 【参考方案1】:

Profile Cell 是一个 UITableViewCell。您不能将其分配给视图。而不是使用

ProfileCell *profilecell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:ProfileTableIdentifier];
    if (profilecell == nil)
                
                    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProfileCell" owner:self options:nil];
                    profilecell = [nib objectAtIndex:0];
                

只用一行

[[NSBundle mainBundle] loadNibNamed:@"ProfileCell" owner:self options:nil]; 

【讨论】:

更新了cellforrowatindexpath的代码,是否有任何修改可以解决重复问题

以上是关于表视图单元格在重新加载表视图和滚动时重复内容的主要内容,如果未能解决你的问题,请参考以下文章

如何更新表格视图单元格?

表视图单元格仅在屏幕外时自动布局

如何修复表视图单元格在启动时不显示?

uitableview 自定义单元格在向下滚动 uitableview 时丢失其内容

重新加载并滚动到特定的集合视图单元格

重新加载表视图数据并取消选择单元格