Tableview单元格在重新加载tableview和滚动时重复内容

时间:2013-12-26 12:31:43

标签: iphone objective-c uitableview ios7 uitextfield

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

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

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

这是我的代码

CODE

- (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;

}

1 个答案:

答案 0 :(得分:-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];