TableView Scroll时单元格内容重叠

时间:2012-03-23 16:18:25

标签: iphone objective-c

当我向上和向下滚动桌面视图时,单元格中的内容会重叠,为什么会这样,我该如何纠正呢?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NS

IndexPath *)indexPath
    {

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

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

        }

            UITextField* tf = nil ;

                switch ( indexPath.row ) {

                    case 0: {

                        cell.textLabel.text = @"First Name" ;

                        tf = firstNameTextField = [self makeTextField:self. placeholder:@"F name"];

                        [cell.contentView addSubview:firstNameTextField];

                        break ;

                    }

                    case 1: {

                        cell.textLabel.text = @"Last Name" ;

                        tf = lastNameTextField = [self makeTextField:self.lastName placeholder:@"L name"];

                        [cell.contentView addSubview:lastNameTextField];

                        break ;

                    }

                    case 2: {

                        cell.textLabel.text = @"age" ;

                        tf = agetextfield = [self makeTextField:self.password placeholder:@"Age"];

                        [cell.contentView addSubview:agetextfield];


                        break ;

                    }


                }
                return cell;

4 个答案:

答案 0 :(得分:1)

在这种情况下重复使用细胞。因此细胞与其他细胞重叠。只需修改您的代码即可解决此问题。

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
        UITextField* tf = nil ;
        for(UIView *view in [cell subViews]){

            if([view isKindOfClass:[UITextFiel class]]){

                  [view removeFromSuperView];
                 }
        }
            switch ( indexPath.row ) {

                case 0: {

                    cell.textLabel.text = @"First Name" ;

                    tf = firstNameTextField = [self makeTextField:self. placeholder:@"F name"];

                    [cell.contentView addSubview:firstNameTextField];

                    break ;

                }

                case 1: {

                    cell.textLabel.text = @"Last Name" ;

                    tf = lastNameTextField = [self makeTextField:self.lastName placeholder:@"L name"];

                    [cell.contentView addSubview:lastNameTextField];

                    break ;

                }

                case 2: {

                    cell.textLabel.text = @"age" ;

                    tf = agetextfield = [self makeTextField:self.password placeholder:@"Age"];

                    [cell.contentView addSubview:agetextfield];


                    break ;

                }


            }
    }


            return cell;

答案 1 :(得分:1)

每次重复使用单元格时,都会向单元格添加UITextField。 我们的想法是仅在创建单元格时添加它,然后从现有单元格中获取现有视图

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
  UITextField* tf = ...;
  tf.tag = kMyTextFieldTag;
  [cell.contentView addSubview:tf];
}
UITextField* tf = (UITextField*)[cell.contentView viewWithTag:kMyTextFieldTag];
tf.text = ...;

更好的想法是让UITextField成为单元格的accessoryView,而不是将其添加为子视图。 如果它没有产生所需的结果,请考虑制作自定义单元格,而不是向UITableViewCellStyleValue1单元格添加视图。

答案 2 :(得分:0)

您的代码很好,但您永远不会从单元格的内容视图中删除“旧”子视图。因此,请在UITextField *tF = nil;之前添加此代码:

for(UIView *v in cell.contentView.subviews){
    [v removeFromSuperview];
}

答案 3 :(得分:0)

您需要将所有子视图(标签等)添加到您的cell.contentview中一次,这意味着您可以修改您的cellForRowAtIndexPath方法,如下所示:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

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

UITextField* tf = nil ;

            switch ( indexPath.row ) {

                case 0: {

                    cell.textLabel.text = @"First Name" ;

                    tf = firstNameTextField = [self makeTextField:self. placeholder:@"F name"];

                    [cell.contentView addSubview:firstNameTextField];

                    break ;

                }

                case 1: {

                    cell.textLabel.text = @"Last Name" ;

                    tf = lastNameTextField = [self makeTextField:self.lastName placeholder:@"L name"];

                    [cell.contentView addSubview:lastNameTextField];

                    break ;

                }

                case 2: {

                    cell.textLabel.text = @"age" ;

                    tf = agetextfield = [self makeTextField:self.password placeholder:@"Age"];

                    [cell.contentView addSubview:agetextfield];


                    break ;

                }

    }





            return cell;
}

每次重新加载单元格时,您都不需要在单元格中添加和删除子视图。

相关问题