UITableView内容在滚动时合并?

时间:2016-01-08 11:15:12

标签: ios objective-c uitableview uibutton uitextfield

我已动态创建UITableView并尝试添加UILabelUIButton,但当我们打开UIViewController时,它会正确显示,当我尝试滚动整个内容时会被搞砸起来。

根据我从服务器获取的字段ID动态添加UITextFieldsUIButtons

这是我的cellForRowAtIndexPath方法

更新

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier;
    if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
        MyIdentifier = @"CharCell";

    }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){
        MyIdentifier = @"IntCell";
    }else{
        MyIdentifier = @"IntCell11";
    }
    cell.textLabel.text = Nil;

    cell= [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[dynamicCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:MyIdentifier];

      }  
        cell.selectionStyle=UITableViewCellSelectionStyleNone;

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
         cell.accessoryType = UITableViewCellAccessoryNone;

        //cell.clipsToBounds=YES;
        if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
            [cell.contentView addSubview:[self getLabel:indexPath.row]];

            [cell.contentView addSubview:[self getVarcharTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]];

        }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){
            [cell.contentView addSubview:[self getLabel:indexPath.row]];

            [cell.contentView addSubview:[self getIntegerTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]];

        }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"SINGLESELECT"]){
            NSString *title=[[selectedTabFields valueForKey:@"name"]objectAtIndex:indexPath.row];

            NSLog(@"single runing");

            [cell.contentView addSubview:[self getSingleSelectLabel:indexPath.item :title]];

            [cell.contentView addSubview:[self addView:indexPath.row :[dynamicOptionFields objectAtIndex:indexPath.row] :[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]];

        }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"MULTISELECT"]){

            NSLog(@"runing");
            [cell.contentView addSubview:[self getLabel:indexPath.row]];

            [cell.contentView addSubview:[self addMultiSelectView:indexPath.row:[dynamicOptionFields objectAtIndex:indexPath.row]:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]];

        }




    return cell;

}

请查看随附的屏幕截图

加载页面enter image description here

当我向下滚动时,它会变成这样 enter image description here

现在它没有合并但是当我向下滚动时我可以看到重复的细胞。 例如:我可以多次看到我的第一行(在滚动结束时)

最新截图enter image description here

2 个答案:

答案 0 :(得分:1)

当您在tableviewcell上一次又一次地添加相同的视图时,无需验证它是否已经存在。相反,您可以添加一次,然后根据您的情况使用隐藏/取消隐藏内容。

还有另一种方法:如果[cell viewWithTag:x](x:Integer)可用,则可以为每个子视图设置标记并验证,然后将其从视图中删除或使用相同的视图。

//cell.clipsToBounds=YES;
        if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
// make check here before adding
            [cell.contentView addSubview:[self getLabel:indexPath.row]];
// make check here before adding
            [cell.contentView addSubview:[self getVarcharTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]];

        }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){
// make check here before adding            
[cell.contentView addSubview:[self getLabel:indexPath.row]];
// make check here before adding
            [cell.contentView addSubview:[self getIntegerTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]];

        }

答案 1 :(得分:0)

请更改以下代码

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier;
    if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
        MyIdentifier = @"CharCell";

    }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){
        MyIdentifier = @"IntCell";
    }else{
        MyIdentifier = @"IntCell11";
    }


    cell= [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[dynamicCell alloc] initWithStyle:UITableViewCellStyleDefault
                                  reuseIdentifier:MyIdentifier];
        }
     cell.textLabel.text = Nil;
           cell.selectionStyle=UITableViewCellSelectionStyleNone;

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
         cell.accessoryType = UITableViewCellAccessoryNone;

        //cell.clipsToBounds=YES;
        if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
            [cell.contentView addSubview:[self getLabel:indexPath.row]];

            [cell.contentView addSubview:[self getVarcharTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]];

        }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){
            [cell.contentView addSubview:[self getLabel:indexPath.row]];

            [cell.contentView addSubview:[self getIntegerTextfield:indexPath.row:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]];

        }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"SINGLESELECT"]){
            NSString *title=[[selectedTabFields valueForKey:@"name"]objectAtIndex:indexPath.row];

            NSLog(@"single runing");

            [cell.contentView addSubview:[self getSingleSelectLabel:indexPath.item :title]];

            [cell.contentView addSubview:[self addView:indexPath.row :[dynamicOptionFields objectAtIndex:indexPath.row] :[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]];

        }else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"MULTISELECT"]){

            [cell.contentView addSubview:[self getLabel:indexPath.row]];

            [cell.contentView addSubview:[self addMultiSelectView:indexPath.row:[dynamicOptionFields objectAtIndex:indexPath.row]:[[selectedTabFields valueForKey:@"value"] objectAtIndex:indexPath.row]]];

    return cell;

}
相关问题