UITableView 内容在滚动时合并?

Posted

技术标签:

【中文标题】UITableView 内容在滚动时合并?【英文标题】:UITableView contents are merging while scrolling? 【发布时间】:2016-01-08 11:15:12 【问题描述】:

我已经动态创建了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;


请查看附件截图

加载页面时

当我向下滚动时,它会变成这样

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

最新截图

【问题讨论】:

if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]) MyIdentifier = @"IntCell"; else MyIdentifier = @"IntCell"; 一旦检查这两个语句都具有相同的标识符,就可以唯一地更改它们 解决它的肮脏方式是cell= [tableView1 dequeueReusableCellWithIdentifier:nil]; 但我再说一遍,这是一种肮脏的方式。 让我试试你的答案 @Bangalore 发布最新的屏幕截图 添加了新的截图 【参考方案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]]];

        

【讨论】:

【参考方案2】:

请修改如下代码

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


【讨论】:

但它如何改变顺序以及如何相互合并? @Bangalore 检查 dynamicCell 连接和初始化的 @Bangalore 也试试这个 cell.clipsToBounds=YES; @Bangalore 你的代码在 put cell= [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier]; 之前更好if (cell == nil) cell = [[dynamicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];其余代码来自 if 条件 if (cell == nil) 没有 else 是什么意思?没看懂?

以上是关于UITableView 内容在滚动时合并?的主要内容,如果未能解决你的问题,请参考以下文章

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

目标 - C:UITableView 内容在滚动时发生变化

设置 UITableView 的内容偏移在滚动时不起作用

关闭另一个视图后 UITableView 滚动内容大小不正确

UITableView 的 shouldIndentWhileEditingRowAtIndexPath: 没有被调用

iOS UITableView在过滤时滚动插入