滚动后Tableview单元格值会发生变化

时间:2014-05-06 08:59:48

标签: ios objective-c uitableview

我在cell条件中设置if(cell == nil)的占位符。

但是,当我滚动表格时,占位符值会互换。请帮忙。

以下是CellForRowAtIndexpath的代码:请告诉我错误的地方..

static NSString *cellIdentifierCell = @"MTRegisterTableViewCell";
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierCell];
        if (cell == nil)
        {


NSArray *nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility 
setXibWithName:cellIdentifierCell] owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;

            if(indexPath.row==6)
            {

                if (cell.txtRegister.text.length == 0) {
                    if (cell.txtRegister.placeholder.length == 0) {
                        cell.txtRegister.placeholder=[self.profileTitlesArray 
objectAtIndex:indexPath.row-1];
                    }
                }
                NSLog(@"Placeholder :%@ && iINDEXPATH.ROW 
:%ld",cell.txtRegister.placeholder,(long)indexPath.row);

            }
            else
            {
                if (cell.txtRegister.text.length == 0) {

                    if (cell.txtRegister.placeholder.length == 0) {
                        cell.txtRegister.placeholder=[self.profileTitlesArray 
objectAtIndex:indexPath.row];
                    }
                }
            }


        }
        if(indexPath.row==5)
        {
            static NSString *cellIdentifierCell = @"MTRegisterDropdownCell";

            MTRegisterDropdownCell *cellDrop = [tableView 
dequeueReusableCellWithIdentifier:cellIdentifierCell];
            if (cellDrop == nil)
            {
                NSArray *nib;
                nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility 
setXibWithName:cellIdentifierCell] owner:self options:nil];
                cellDrop = (MTRegisterDropdownCell *)[nib objectAtIndex:0];
                [cellDrop configureCell:nil];
            }
            self.countryCell=cellDrop;
            cellDrop.clipsToBounds=NO;

            self.tblDropdown.clipsToBounds=NO;
            [cellDrop.btnCountry addTarget:self action:@selector(btnCountryClicked:) 
forControlEvents:UIControlEventTouchUpInside];
            [cellDrop.btnState addTarget:self action:@selector(ButtonStateClicked:) 
forControlEvents:UIControlEventTouchUpInside];
            if(self.strSelectedCountry)
            {
                [cellDrop.txtCountry setText:self.strSelectedCountry];
            }
            else{
                if ([self.countriesArray containsObject:kDefaultCountry]) {
                    [cellDrop.txtCountry setText:kDefaultCountry];
                    NSInteger index = [self.countriesArray 
indexOfObject:kDefaultCountry];
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index 
inSection:0];
                    self.tblDropdown.tag=TABLECOUNTRYTAG;
                    [self tableView:self.tblDropdown didSelectRowAtIndexPath:indexPath];

                }

            }
            if(self.strSelectedState)
            {
                [cellDrop.txtState setText:self.strSelectedState];
            }
            return cellDrop;
        }

        else
        {
            if (cell.txtRegister.text.length == 0) {

                NSLog(@"Placeholder :%@",cell.txtRegister.placeholder);
                if ([cell.txtRegister.placeholder isEqualToString:@"City"]) {
                    if (self.strSelectedCity) {
                        cell.txtRegister.text = self.strSelectedCity;
                    }
                    else
                        cell.txtRegister.text=[self.profileTitlesArray 
objectAtIndex:indexPath.row];
                    NSLog(@"profileTitlesArray :%@",self.profileTitlesArray);
                    NSLog(@"indexpath.Row :%ld && cell TEXT :%@",
(long)indexPath.row,cell.txtRegister.text);
                    BaseButton *aBtn = [[BaseButton alloc] 
initWithFrame:CGRectMake(cell.txtRegister.frame.origin.x-50, 
cell.txtRegister.frame.origin.y-10, cell.txtRegister.frame.size.width+50,   
cell.txtRegister.frame.size.height+50)];
                    [aBtn setBackgroundColor:[UIColor clearColor]];
                    [cell.txtRegister addSubview:aBtn];
                    [aBtn addTarget:self action:@selector(btnCitiesClicked:) 
forControlEvents:UIControlEventTouchUpInside];
                    self.cityCelll = cell;
                    cell.clipsToBounds = NO;
                }
            }
            else{
                if ([cell.txtRegister.placeholder isEqualToString:@"City"]) {
                    if (self.strSelectedCity) {
                        cell.txtRegister.text = self.strSelectedCity;
                    }
                }
            }

        }

        [cell configureCell:registerModel];
        return cell;
    }

2 个答案:

答案 0 :(得分:0)

没有读完整个代码,但听起来你需要在重复使用单元格之前重置占位符,它们不一定在滚动时重新创建,这就是表的重点毕竟看。

-[UITableViewCell prepareForReuse]。它在出列/重用现有表格单元格时被调用,因此从技术上讲,当您调用-[UITableView dequeueReusableCellWithIdentifier:]时(您需要子类UITableViewCell,它会进一步组织您的代码)。

答案 1 :(得分:0)

我终于通过使用以下行解决了它:

            NSString *cellIdentifierCell = [NSString 
stringWithFormat:@"MTRegisterTableViewCell %ld",(long)indexPath.row];
            cell = (MTRegisterTableViewCell *)[tableView 
dequeueReusableCellWithIdentifier:cellIdentifierCell];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility 
setXibWithName:@"MTRegisterTableViewCell"] owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;